【问题标题】:Cannot use .dll from OpenCV with CMake无法将 OpenCV 中的 .dll 与 CMake 一起使用
【发布时间】:2020-07-26 07:21:46
【问题描述】:

我有以下CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project( project_360_visual )
find_package( OpenCV REQUIRED )
set(SOURCE_FILES 
    ${CMAKE_SOURCE_DIR}/src/project_360_visual.cpp
    ${CMAKE_SOURCE_DIR}/src/projection.cpp)
set(INCLUDE_FILES
    ${CMAKE_SOURCE_DIR}/include/project_360_visual.h
    ${CMAKE_SOURCE_DIR}/include/projection.h)
LINK_DIRECTORIES(c:/opencv/build/bin/Release)
add_library(opencv_tracking430 SHARED IMPORTED GLOBAL)
set_target_properties(opencv_tracking430 PROPERTIES IMPORTED_LOCATION c:/opencv/build/bin/Release/opencv_tracking430.dll)
add_executable( project_360_visual ${SOURCE_FILES} ${INCLUDE_FILES})
target_include_directories(project_360_visual PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries( project_360_visual PUBLIC ${OpenCV_LIBS} opencv_tracking430)

相关的 Visual Studio 项目是使用以下方法生成的:

cmake ../ -G "Visual Studio 15 2017 Win64" -DCMAKE_PREFIX_PATH=c:/opencv/build

但是,无论我做什么,我都无法正确链接/导入 OpenCV 中的共享库。有人可以帮我解决这个问题吗?

这是我尝试构建时 Visual Studio 的当前输出:

1>------ Build started: Project: ZERO_CHECK, Configuration: Release x64 ------
1>Checking Build System
1>CMake is re-running because C:/Projects/candido/CG/build/CMakeFiles/generate.stamp is out-of-date.
1>  the file 'C:/Projects/candido/CG/CMakeLists.txt'
1>  is newer than 'C:/Projects/candido/CG/build/CMakeFiles/generate.stamp.depend'
1>  result='-1'
1>-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.
1>-- Configuring done
1>-- Generating done
1>-- Build files have been written to: C:/Projects/candido/CG/build
2>------ Build started: Project: project_360_visual, Configuration: Release x64 ------
2>LINK : fatal error LNK1181: cannot open input file 'opencv_tracking430-NOTFOUND.obj'
2>Done building project "project_360_visual.vcxproj" -- FAILED.
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

更新

根据建议尝试了以下两种变体。

变体 1:以下内容无法编译:

cmake_minimum_required(VERSION 3.12)
project( project_360_visual )
find_package( OpenCV REQUIRED )
set(SOURCE_FILES 
    ${CMAKE_SOURCE_DIR}/src/project_360_visual.cpp
    ${CMAKE_SOURCE_DIR}/src/projection.cpp)
set(INCLUDE_FILES
    ${CMAKE_SOURCE_DIR}/include/project_360_visual.h
    ${CMAKE_SOURCE_DIR}/include/projection.h)

#Linking shared libraries
add_library(opencv_tracking430 IMPORTED GLOBAL)
set_target_properties(opencv_tracking430 PROPERTIES IMPORTED_LOCATION 
    c:/opencv/build/bin/Release/opencv_tracking430.lib
)

add_executable( project_360_visual ${SOURCE_FILES} ${INCLUDE_FILES})
target_include_directories(project_360_visual PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries( project_360_visual PUBLIC ${OpenCV_LIBS} opencv_tracking430)

错误:

1>------ Build started: Project: ZERO_CHECK, Configuration: Release x64 ------
1>Checking Build System
1>CMake is re-running because C:/Projects/candido/CG/build/CMakeFiles/generate.stamp is out-of-date.
1>  the file 'C:/Projects/candido/CG/CMakeLists.txt'
1>  is newer than 'C:/Projects/candido/CG/build/CMakeFiles/generate.stamp.depend'
1>  result='-1'
1>-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.
1>CMake Error at CMakeLists.txt:13 (add_library):
1>  add_library called with IMPORTED argument but no library type.
1>
1>
1>CMake Error at CMakeLists.txt:14 (set_target_properties):
1>  set_target_properties Can not find target to add properties to:
1>  opencv_tracking430
1>
1>
1>-- Configuring incomplete, errors occurred!
1>See also "C:/Projects/candido/CG/build/CMakeFiles/CMakeOutput.log".
1>CMake Configure step failed.  Build files cannot be regenerated correctly.  Attempting to stop IDE build.
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets(209,5): error MSB6006: "cmd.exe" exited with code 1.
1>Done building project "ZERO_CHECK.vcxproj" -- FAILED.
2>------ Build started: Project: project_360_visual, Configuration: Release x64 ------
2>Building Custom Rule C:/Projects/candido/CG/CMakeLists.txt
2>CMake is re-running because C:/Projects/candido/CG/build/CMakeFiles/generate.stamp is out-of-date.
2>  the file 'C:/Projects/candido/CG/CMakeLists.txt'
2>  is newer than 'C:/Projects/candido/CG/build/CMakeFiles/generate.stamp.depend'
2>  result='-1'
2>-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.
2>CMake Error at CMakeLists.txt:13 (add_library):
2>  add_library called with IMPORTED argument but no library type.
2>
2>
2>CMake Error at CMakeLists.txt:14 (set_target_properties):
2>  set_target_properties Can not find target to add properties to:
2>  opencv_tracking430
2>
2>
2>-- Configuring incomplete, errors occurred!
2>See also "C:/Projects/candido/CG/build/CMakeFiles/CMakeOutput.log".
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets(209,5): error MSB6006: "cmd.exe" exited with code 1.
2>Done building project "project_360_visual.vcxproj" -- FAILED.
========== Build: 0 succeeded, 2 failed, 0 up-to-date, 0 skipped ==========

变体 2

这会编译,但在运行时它会抱怨找不到相关的.dll

cmake_minimum_required(VERSION 3.12)
project( project_360_visual )
find_package( OpenCV REQUIRED )
set(SOURCE_FILES 
    ${CMAKE_SOURCE_DIR}/src/project_360_visual.cpp
    ${CMAKE_SOURCE_DIR}/src/projection.cpp)
set(INCLUDE_FILES
    ${CMAKE_SOURCE_DIR}/include/project_360_visual.h
    ${CMAKE_SOURCE_DIR}/include/projection.h)

#Linking shared libraries
add_library(opencv_tracking430 SHARED IMPORTED GLOBAL)
set_target_properties(opencv_tracking430 PROPERTIES IMPORTED_LOCATION 
    c:/opencv/build/bin/Release/opencv_tracking430.dll
)
set_target_properties(opencv_tracking430 PROPERTIES IMPORTED_IMPLIB
    c:/opencv/build/lib/Release/opencv_tracking430.lib
)

add_executable( project_360_visual ${SOURCE_FILES} ${INCLUDE_FILES})
target_include_directories(project_360_visual PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries( project_360_visual PUBLIC ${OpenCV_LIBS} opencv_tracking430)

【问题讨论】:

  • 在使用 DLL 时,您需要包含 .lib 文件的路径以进行链接。
  • .lib不是windows中的静态库吗?我很困惑(我通常链接静态库)。
  • 我的意思是,在 Windows 上使用动态库 (DLL) 时,您需要提供 .lib 的路径以及 .lib 库文件名。链接需要 .lib 文件本身。
  • 如何在 CMake 中提供路径?我刚刚尝试在LINK_DIRECTORIES 中添加.libs 的路径。还是不行。
  • 在变体 2 中,问题已解决。但是,可执行文件需要知道在哪里可以找到 DLL。例如,您可以将 DLL 复制到与可执行文件相同的目录,或更新您的 Path 环境变量。

标签: windows dll cmake visual-studio-2017 linker-errors


【解决方案1】:

您不链接到 DLL 库。这些文件用于运行时加载程序。通常,DLL 将与 .lib 导入库配对,您可以使用它来进行链接。导入和链接到带有配对 .lib 的 DLL 的 CMake 语法如下(也使用 IMPORTED_IMPLIB):

add_library(opencv_tracking430 SHARED IMPORTED GLOBAL)
set_target_properties(opencv_tracking430 PROPERTIES IMPORTED_LOCATION 
    c:/opencv/build/bin/Release/opencv_tracking430.dll
)
set_target_properties(opencv_tracking430 PROPERTIES IMPORTED_IMPLIB
    c:/opencv/build/bin/Release/opencv_tracking430.lib
)

【讨论】:

  • 它不起作用,让我用更改更新问题。
  • 我认为在您的第二个提案中,您可能缺少SHAREDSTATIC 关键字,在您的add_library 指令中,我的意思是,我尝试使用SHARED。我可以编译,但仍然找不到.dll。
  • @user8469759 你是对的,add_library() 语法现在似乎需要库类型。我已删除第二个建议。
  • 但是第一个还不行。我不知道为什么它找不到.dll,尽管我指定了它在哪里。
  • @user8469759 如果你的程序编译/链接成功,那么剩下的问题就是一个单独的问题。这是一个运行时加载程序问题,您可以按照我在上面的评论中的建议或通过 CMake(参见 this 帖子)解决。
猜你喜欢
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
  • 1970-01-01
  • 2020-09-19
  • 2014-01-30
  • 2015-04-08
  • 2019-01-07
相关资源
最近更新 更多