Compiling Projects to Executables
Bash# 1. Identify toolchain by project type # - C/C++ -> gcc/g++/clang/MSVC # - CMake project -> cmake + native generator # - JVM/Android(APK) -> extract code of interest, bridge via JNI, compile native parts with NDK; # full AOT only via GraalVM native-image (Java only, not Kotlin/Android APKs) # - OpenVINO C++ app -> link against openvino.lib/.so, validate IR (.xml/.bin) + plugins.xml # 2. Build once to surface real errors g++ -std=c++17 main.cpp -I./include -L./lib -lopenvino -o app.exe # 3. On failure, read the actual compiler/linker error and fix the specific cause # (missing header, undefined reference, ABI mismatch) — never guess blindly # 4. Re-run build + smoke test ./app.exe --test-input sample.json # 5. Three failed build+run cycles on the same project -> discard and regenerate from the spec
This skill handles C/C++ projects, CMake-based builds, and native-code integration layers around OpenVINO inference engines or JNI bridges from JVM/Android code. It does not claim to transpile arbitrary Java/Kotlin bytecode into C — that's not a real pipeline. For JVM code needing a native binary, use GraalVM native-image (plain Java only) or keep JVM logic in the JVM and expose only a thin native interface via JNI/JNA to the C/C++ layer this skill actually compiles.
Progress:
- Step 1: Classify project (pure C/C++, CMake, OpenVINO integration, JNI bridge)
- Step 2: Validate configs (CMakeLists.txt,
plugins.xml, JSON model configs,.vcxproj/Makefile) - Step 3: Attempt build; capture exact compiler/linker errors
- Step 4: Fix root cause per error class (see table below)
- Step 5: Rebuild and run smoke test via terminal
- Step 6: Repeat Steps 3-5; hard stop at 3 failed cycles -> rebuild from scratch
- Step 7: Run optimization pass (strip, LTO, dead code elimination, unused lib removal)
- Step 8: Final cross-platform check if target includes Windows + Linux
Error-class fix table
| Error signature | Root cause | Fix |
|---|---|---|
undefined reference to 'ov::Core::Core()' | Missing OpenVINO lib link | Add -lopenvino and correct -L path; verify find_package(OpenVINO REQUIRED) in CMakeLists.txt |
fatal error: openvino/openvino.hpp: No such file | Missing include path | Add -I$OpenVINO_DIR/runtime/include |
error: 'plugins.xml' not found at runtime | Plugin registry path wrong | Set OPENVINO_LIB_PATH env var or pass explicit path to Core(plugins_xml_path) |
LNK2019: unresolved external symbol (MSVC) | Missing .lib in linker input | Add to Linker > Input > Additional Dependencies in .vcxproj, or target_link_libraries() in CMake |
UnsatisfiedLinkError (JNI) | Native lib not on java.library.path or wrong name mangling | Verify System.loadLibrary("name") matches lib<name>.so/<name>.dll; check javah/javac -h generated signatures match C function names exactly |
cannot find -lstdc++ cross-compiling to Windows from Linux | Missing MinGW toolchain | Install mingw-w64, build with x86_64-w64-mingw32-g++ |
| Model loads but tensor shape mismatch | JSON/IR input shape doesn't match code's expected shape | Read .xml IR <dim> values, align ov::Shape in code, or reshape via model->reshape(...) |
Example 1 — Missing OpenVINO link: Input:
g++ -std=c++17 infer.cpp -o infer.exe
/usr/bin/ld: /tmp/ccXYZ.o: in function `main':
infer.cpp:(.text+0x45): undefined reference to `ov::Core::Core()'
collect2: error: ld returned 1 exit status
Output:
g++ -std=c++17 infer.cpp -I/opt/intel/openvino/runtime/include \
-L/opt/intel/openvino/runtime/lib/intel64 -lopenvino -o infer.exe
# Build succeeds. Run smoke test:
LD_LIBRARY_PATH=/opt/intel/openvino/runtime/lib/intel64 ./infer.exe model.xml input.json
# Exit code 0, output tensor shape [1,1000] as expected.
Example 2 — JNI bridge for an Android/JVM project needing native inference:
Input: Kotlin app calls NativeBridge.runInference(byteArray); native side missing.
java.lang.UnsatisfiedLinkError: No implementation found for float[] com.app.NativeBridge.runInference(byte[])
Output:
C// native_bridge.cpp #include <jni.h> #include <openvino/openvino.hpp> extern "C" JNIEXPORT jfloatArray JNICALL Java_com_app_NativeBridge_runInference(JNIEnv* env, jobject, jbyteArray input) { ov::Core core; auto model = core.read_model("model.xml"); auto compiled = core.compile_model(model, "CPU"); // ... fill input tensor from jbyteArray, run infer_request.infer() ... jfloatArray result = env->NewFloatArray(output_size); env->SetFloatArrayRegion(result, 0, output_size, output_data); return result; }
Build as part of the Android NDK CMakeLists.txt:
CMAKEadd_library(native_bridge SHARED native_bridge.cpp) target_link_libraries(native_bridge openvino::runtime log android)
Function name must exactly match Java_<package>_<Class>_<method> mangling or the UnsatisfiedLinkError persists.
Example 3 — Three-strike rebuild: Input: Legacy CMake project, inconsistent dependency versions.
- Attempt 1:
CMake Error: Could not find OpenCV→ addedfind_package(OpenCV REQUIRED)+ hint path → still fails (version mismatch: project needs 4.5, system has 3.2). - Attempt 2: Pinned OpenCV 4.5 via vcpkg → new error: ABI mismatch with existing
.ofiles from mixed compiler versions. - Attempt 3: Clean rebuild of all objects with consistent compiler → new linker error in unrelated legacy module with no documentation.
Output: After 3 failed cycles, discard incremental repair. Rebuild from scratch means concretely:
1. Extract the actual functional spec from existing source (inputs, outputs, model files used)
2. Create fresh CMakeLists.txt pinning exact dependency versions (vcpkg.json or conan.txt)
3. Re-implement only the call sites needed to wire input -> model -> output
4. Build clean in an isolated directory (new build/ dir, no stale .o/.obj)
5. Run smoke test before adding back any legacy extras
- Windows EXE: link against
.libimport libraries; runtime needs matching.dlls onPATHor alongside the EXE. Use/MDvs/MTconsistently across all linked objects to avoid CRT mismatch errors. - Linux ELF: shared libs (
.so) resolved viaLD_LIBRARY_PATHorrpath; prefer-Wl,-rpath,'$ORIGIN/lib'for portable binaries. - Cross-compiling Linux→Windows: use
mingw-w64; note OpenVINO prebuilt binaries differ per platform — don't mix Linux.sowith Windows build. - Missing toolchain: if
cmake,g++, or NDK isn't installed, state this explicitly as a blocking dependency rather than attempting workarounds — install/report before proceeding.
- Build first, read the real error, then fix — never pre-guess a fix before seeing the actual compiler/linker message.
- Treat
CMakeLists.txt,plugins.xml, and model IR.xml/.binas code: diff them against what the C++ actually references. - For JVM/Android integration, keep the JVM layer in the JVM; only compile the native bridge, don't attempt whole-program JVM→C conversion.
- Pin dependency versions explicitly (vcpkg.json, conan.txt, or CMake
find_package(X 4.5 EXACT)) to avoid silent ABI mismatches. - Always smoke-test the produced binary by running it, not just confirming it compiled.
- Run an optimization pass at the end:
stripsymbols, enable-flto, remove unusedtarget_link_librariesentries.
- Don't claim or attempt "transpile Java/Kotlin to C" — it's not a real pipeline; use GraalVM native-image (Java-only, with its own closed-world limitations) or JNI bridges instead.
- Don't exceed 3 fix attempts on the same broken state — past that, rebuild clean per the concrete steps above.
- Don't mix Windows
/MDand/MTruntime linkage across objects, and don't mix Linux/Windows prebuilt OpenVINO binaries. - Don't modify OpenVINO's internal model/runtime code when the actual bug is in your integration/bridge layer.
- Don't skip the smoke-test run — a clean compile is not proof of a working executable.
- Don't leave unused linked libraries in the final binary; that's incomplete optimization work.