与OpenCV和CMake的链接问题 [英] Linking problem with OpenCV and CMake

查看:147
本文介绍了与OpenCV和CMake的链接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用OpenCV的程序(安装在本地目录中,因为我在那台计算机上没有root权限),并且我已经编写了相应的CMakeLists.txt文件.我的问题是,编译在链接阶段以不同的方式失败(我花了三个小时尝试了Web上提出的所有不同解决方案,所以我看到了很多结果). 这里是对我来说更有意义的配置/结果,即使它们会导致失败: [在project_root/CMakeLists.txt中]:

I am writing a program which uses OpenCV (installed in a local directory, since I don't have root permissions on that machine), and I have written the corresponding CMakeLists.txt file. My problem is that the compilation fails at the linking stage in different ways (I've spent three hours trying all the different solutions proposed on the web, so I've seen plenty of outcomes). Here you are the configurations/results which make more sense to me, even though they lead to a failure: [in 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中]:

[in 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":

If, contrary to the recommendations given on the net, I put a "-l" before the OpenCV library name I get:

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

有人知道如何解决这个问题吗?我真的为此感到疯狂... 在此先多谢! 干杯, 罗布

Does anyone know how to solve this thing? I am literally driving crazy on this... Thanks a lot in advance! Cheers, Rob

推荐答案

此行:

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变量中有一个库,其名称巨大,其中有很多空格.如果删除双引号,如下所示:

...says that you have one library in the OpenCV_LIBS variable with a gigantic name with lots of spaces in it. If you remove the double quotes, like this:

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)

...然后这将是一个库名列表,并且应该可以正常工作.

...then it will be a list of library names, and it should work just fine.

CMake使用空格字符将参数分隔为其命令.因此,要在set命令的值中包含空格,您必须将其双引号引起来.在这种情况下,您不需要这样做,因为您希望set命令查看您要传入的多个值,而不是一个包含空格的大值.

CMake uses the space character to separate arguments to its commands. Because of that, to include a space in a value in the set command, you have to double quote it. In this case, you don't want to do that because you want the set command to see the multiple values you're passing in, not one big value that contains spaces.

(基于评论中的讨论获得的更多信息):

(further information based on discussion in the comments):

如果这仍然不起作用,请仔细检查您使用的名称是否正确.使用您在此处使用的库名称,在由OpenCV_LIBS_DIR变量命名的目录中应该有名为libopencv_core.so,libopencv_imgproc.so等的文件.如果这些确切的库名不作为文件存在,则说明链接器错误. (在这种情况下,如评论中所述,实际文件在文件名中用版本号命名,并且没有指向它们的非版本"符号链接.)

If this still doesn't work, double-check that the names you are using are correct. With the library names you're using here, there should be files named libopencv_core.so, libopencv_imgproc.so, and so on, in the directory named by the OpenCV_LIBS_DIR variable. If those exact library names do not exist as files, then that explains the linker error. (In this case, as discussed in the comments, the actual files were named with version numbers in the file names and there were no "non-versioned" symlinks pointing to them.)

这篇关于与OpenCV和CMake的链接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆