0

I want to use the prebuilt onnxruntime library with cmake in c++. This my directory structure:

/root/externals/onnxruntime/include
/root/externals/onnxruntime/lib
/root/CMakeLists.txt 
/root/main.cpp 

The header files are in the include folder and in the lib folder are *.lib, *.dll, *.pdb files.

This is the CMakeLists.txt, I wrote:

cmake_minimum_required(VERSION 3.13)
project(myproject VERSION 0.1.0 LANGUAGES C CXX)

add_executable(myproject  main.cpp)

set(ONNXRUNTIME_ROOTDIR ${CMAKE_SOURCE_DIR}/externals/onnxruntime)
find_library(myonnxruntimelib onnxruntime NAME onnxruntime HINTS 
${CMAKE_SOURCE_DIR}/externals/onnxruntime/lib)

if(${myonnxruntimelib} STREQUAL myonnxruntimelib-NOTFOUND)
message(FATAL_ERROR "onnxruntime not found")
else()
message(STATUS "onnxruntime found as ${myonnxruntimelib}")
endif()

include_directories("${ONNXRUNTIME_ROOTDIR}/include")

target_link_libraries(myproject ${myonnxruntimelib})

The find_library() finds the *.lib file correctly, but when I build it, it gives these errors:

main.obj : error LNK2019: unresolved external symbol _OrtGetApiBase@0 referenced in function "void __cdecl `dynamic initializer for 'public: static struct OrtApi const * const Ort::Global< void>::api_''(void)" (??__E?api_@?$Global@X@Ort@@2PBUOrtApi@@B@@YAXXZ) 
[C:\root\build\myproject.vcxproj] C:\root\externals\onnxruntime\lib\onnxruntime.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86' [C:\root\build\myproject.vcxproj] 
C:\root\build\Debug\myproject.exe : fatal error LNK1120: 1 unresolved externals [C:\root\build\myproject.vcx  proj]
Done Building Project "C:\root\build\myproject.vcxproj" (default targets) -- FAILED.


Build FAILED.

 "C:\root\build\myproject.vcxproj" (default target) (1) ->(Link target) ->
C:\root\externals\onnxruntime\lib\onnxruntime.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86' [C:\root\build\myproject.vcxproj]

"C:\root\build\myproject.vcxproj" (default target) (1) ->(Link target) ->

 main.obj : error LNK2019: unresolved external symbol _OrtGetApiBase@0 referenced in function "void __cdecl `dynamic initializer for 'public: static struct OrtApi const * const Ort::Global<void>::api_''(void)" (??__E?api_@?$Global@X@Ort@@2PBUOrtApi@@B@@YAXXZ) 
[C:\root\build\myproject.vcxproj]C:\root\build\Debug\myproject.exe : fatal error LNK1120: 1 unresolved externals [C:\root\build\myproject.v cxproj]

1 Warning(s)
2 Error(s)

Moreover, onnxruntime.lib file has this structure

enter image description here

Any hint how this issue can be resolved?

user51780
  • 139
  • 2
  • 5
  • Did you try some tips from https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix ? After `find_library` can you try `add_library(myonnx SHARED IMPORTED GLOBAL)` and set these properties: INTERFACE_INCLUDE_DIRECTORIES, IMPORTED_LOCATION (path to dll), IMPORTED_IMPLIB (path do lib) – pptaszni Jul 17 '23 at 11:05
  • https://cmake.org/cmake/help/book/mastering-cmake/chapter/Finding%20Packages.html#creating-cmake-package-configuration-files this might also be interesting if you want to create your own `CMakeFindXXX` and simply use `find_package` afterwards. – pptaszni Jul 17 '23 at 11:18
  • Thank you for the feedback. I tried the suggestion and it did not work! Note that I did not build the onnxruntime. It is already build which can be found here " https://github.com/microsoft/onnxruntime/releases/" – user51780 Jul 17 '23 at 16:21
  • ***I tried the suggestion and it did not work!*** Maybe if you show what you tried someone can help fix it. – drescherjm Jul 17 '23 at 16:35
  • Maybe `_OrtGetApiBase` is not in `onnxruntime.lib` you can use [https://stackoverflow.com/questions/305287/how-to-see-the-contents-of-windows-library-lib](https://stackoverflow.com/questions/305287/how-to-see-the-contents-of-windows-library-lib) to help you see what is in the `.lib` file. Also was that the only error? – drescherjm Jul 17 '23 at 16:37
  • It seems onnxruntime.lib has _OrtGetApiBase. I got two errors actually. I edited the question by including the error messages in detail . – user51780 Jul 17 '23 at 21:23
  • In your edited question I still don't see `add_library(onnxruntime SHARED IMPORTED)`. Did you try to define imported target and link against it? It is supposed to be used with already compiled external libraries. – pptaszni Jul 18 '23 at 07:23
  • I am not professional in CMake. My question is that why do we need to use add_library(onnxruntime SHARED IMPORTED)? In other words, wouldn't be enough to use find_libray() to find a prebuilt library in to the project and then link it? Btw, I tried add_library(onnxruntime SHARED IMPORTED) but it didt work so I removed it. – user51780 Jul 18 '23 at 19:55

0 Answers0