如何在windows中通过cmake使用onnxruntime库?

问题描述 投票:0回答:1

我想在 C++ 中使用带有 cmake 的预构建 onnxruntime 库。 这是我的目录结构:

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

头文件位于include文件夹中,lib文件夹中为*.lib、*.dll、*.pdb文件。

这是CMakeLists.txt,我写道:

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})

find_library() 正确找到 *.lib 文件,但是当我构建它时,它给出了这些错误:

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)

此外,onnxruntime.lib文件具有这样的结构

enter image description here

有什么提示可以解决这个问题吗?

c++ windows cmake onnx onnxruntime
1个回答
0
投票

我有这个工作 CMakeLists.txt 文件:

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

add_executable(myproject  main.cpp)

set(ONNXRUNTIME_LIBRARIES 
    "onnxruntime.lib" 
)

target_include_directories(myproject PUBLIC
    "${ONNXRUNTIME_ROOTDIR}/include"                           # Pre-built package
    "${ONNXRUNTIME_ROOTDIR}/include/onnxruntime"               # Linux local install to /usr/local
    "${ONNXRUNTIME_ROOTDIR}/include/onnxruntime/core/session"  # Windows local install
)

target_link_directories(myproject PUBLIC "${ONNXRUNTIME_ROOTDIR}/lib")

target_link_libraries(myproject PUBLIC 
    ${ONNXRUNTIME_LIBRARIES} 
)

您所要做的就是将

ONNXRUNTIME_ROOTDIR
宏设置为 ONNXRUNTIME 的根目录。

例如,下载此版本onnxruntime-win-x64-1.17.1.zip并解压,然后使用此路径

-DONNXRUNTIME_ROOTDIR="path/to/onnxruntime-win-x64-1.17.1"

© www.soinside.com 2019 - 2024. All rights reserved.