我正在编写一个使用OpenCV的程序(安装在本地目录中,因为我没有该机器上的root权限),并且我编写了相应的CMakeLists.txt文件。我的问题是编译在链接阶段以不同的方式失败(我花了三个小时尝试在网上提出的所有不同的解决方案,所以我看到了很多结果)。 在这里,您是对我更有意义的配置/结果,即使它们导致失败: [在project_root / CMakeLists.txt中]:
cmake_minimum_required(VERSION 2.8)
project(CUDA_learning)
set(OpenCV_INCLUDE_DIR "path/to/opencv_CUDA/include")
include_directories(${OpenCV_INCLUDE_DIR})
set(OpenCV_LIBS_DIR "path/to/opencv_CUDA/lib")
link_directories(${OpenCV_LIBS_DIR})
set(OpenCV_LIBS "opencv_core opencv_imgproc opencv_calib3d opencv_video opencv_features2d opencv_ml opencv_highgui opencv_objdetect opencv_contrib opencv_legacy opencv_gpu")
find_package(Boost COMPONENTS system filesystem program_options regex REQUIRED)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIR})
else(Boost_FOUND)
message(FATAL_ERROR "Cannot build application without Boost. Please set Boost_INCLUDE_DIR.")
endif(Boost_FOUND)
set(CMAKE_BUILD_TYPE debug)
add_definitions("-Wall")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/../bin)
subdirs (
../src
)
[在project_root / src / CMakeLists.txt中]:
FILE(GLOB dir_source *.cc 2D/*.cc)
FILE(GLOB dir_header *.hh 2D/*.hh)
add_executable(${PROJECT_NAME} ${dir_source} ${dir_header})
target_link_libraries(${PROJECT_NAME} ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${OpenCV_LIBS})
结果:
Linking CXX executable ../../bin/CUDA_learning
c++: opencv_imgproc: No such file or directory
c++: opencv_calib3d: No such file or directory
c++: opencv_video: No such file or directory
c++: opencv_features2d: No such file or directory
c++: opencv_ml: No such file or directory
c++: opencv_highgui: No such file or directory
c++: opencv_objdetect: No such file or directory
c++: opencv_contrib: No such file or directory
c++: opencv_legacy: No such file or directory
c++: opencv_gpu: No such file or directory
如果与网上给出的建议相反,我在OpenCV库名称之前放了一个“-l”:
Linking CXX executable ../../bin/CUDA_learning
/usr/bin/ld: cannot find -lopencv_core
collect2: ld returned 1 exit status
make[2]: *** [../bin/CUDA_learning] Error 1
make[1]: *** [src/CMakeFiles/CUDA_learning.dir/all] Error 2
make: *** [all] Error 2
有谁知道如何解决这个问题?我真的对此疯狂...... 非常感谢提前! 干杯, 罗布
答案 0 :(得分:5)
这一行:
set(OpenCV_LIBS "opencv_core opencv_imgproc opencv_calib3d opencv_video opencv_features2d opencv_ml opencv_highgui opencv_objdetect opencv_contrib opencv_legacy opencv_gpu")
...说你在OpenCV_LIBS变量中有一个库,其中有一个巨大的名称,里面有很多空格。如果你删除双引号,如下所示:
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_calib3d opencv_video opencv_features2d opencv_ml opencv_highgui opencv_objdetect opencv_contrib opencv_legacy opencv_gpu)
...然后它将是一个库名称列表,它应该可以正常工作。
CMake使用空格字符将参数与其命令分开。因此,要在set命令中的值中包含空格,必须对其进行双引号。在这种情况下,您不希望这样做,因为您希望set命令查看您传入的多个值,而不是一个包含空格的大值。
编辑:(根据评论中的讨论提供更多信息):
如果仍然无效,请仔细检查您使用的名称是否正确。使用此处使用的库名称,在OpenCV_LIBS_DIR变量指定的目录中应该有名为libopencv_core.so,libopencv_imgproc.so等文件。如果那些确切的库名称不作为文件存在,那么这就解释了链接器错误。 (在这种情况下,正如评论中所讨论的那样,实际文件以文件名中的版本号命名,并且没有指向它们的“非版本化”符号链接。)
答案 1 :(得分:5)
以下是将OpenCV包含在项目中的正确方法:
set(OpenCV_DIR "path/to/opencv_CUDA/share/OpenCV"
CACHE PATH "The path where OpenCVConfig.cmake is placed")
find_package(OpenCV REQUIRED)
# and for each executable/library dependent on OpenCV
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS} )
答案 2 :(得分:0)
问题可能是链接器正在查找名为libopencv_core.so
的文件但是有一个类似名称libopencv_core.so.1
的文件,在这种情况下您需要使用符号链接。例如,执行此ln -sf /opt/lib/libopencv_core.so.1 /opt/lib/libopencv_core.so