无法在CMake中使用opencv中的.dll

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

我有以下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 ==========
windows opencv dll cmake visual-studio-2017
1个回答
0
投票

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

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 ) ),因为CMake足够聪明,可以确定.lib目标为IMPORTED

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