简体   繁体   English

OpenCV:使用cmake时对`cv :: imread的未定义引用

[英]OpenCV: undefined reference to `cv::imread when using cmake

I have to use OpenCV on two systems, both Linux. 我必须在两个系统(都是Linux)上使用OpenCV。 My setup works on the first system, but fails on the second. 我的设置可在第一个系统上运行,但在第二个系统上失败。 For both systems I have installed OpenCV in a custom location using: 对于这两个系统,我都使用以下命令在自定义位置安装了OpenCV:

cd myBuildDir
cmake -DWITH_VTK=OFF -DCMAKE_BUILD_TYPE=Release 
  -DCMAKE_INSTALL_PREFIX=~/Software/OpenCVLibs/opencv-3.1.0/release 
  ~/Software/opencv-3.1.0 
make && make install

and similar for debug. 和调试类似。 The directories are different. 目录是不同的。 One is with my user on my laptop, the other with a different user on a multiuser system. 一个是在笔记本电脑上与我的用户一起使用,另一个是在多用户系统上与另一个用户一起使用。

I then used the example given in the OpenCV documentation to test my setup: http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html 然后,我使用OpenCV文档中给出的示例来测试我的设置: http : //docs.opencv.org/2.4/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html

To build the example using my custom OpenCV location I used 使用我自定义的OpenCV位置构建示例

cmake -DCMAKE_BUILD_TYPE=Release -DOpenCV_DIR=~/Software/OpenCVLibs
  /opencv-3.1.0/release/share/OpenCV exampleSourceDir

This works on the first system (gcc version 5.3.0) but fails on the second (gcc version 4.6.3). 这在第一个系统(gcc版本5.3.0)上有效,但在第二个系统(gcc版本4.6.3)上无效。 Don't think the gcc version matter here, but stating them for completeness. 不要以为gcc版本在这里很重要,但请说出它们的完整性。 I think it might be related to that on the first system there is no OpenCV in the system path installed, while in the second system there is. 我认为这可能与在第一个系统上没有安装OpenCV的系统路径有关,而在第二个系统中有。 However the build fails with: 但是,构建失败并显示:

/usr/bin/cmake -E cmake_link_script CMakeFiles/DisplayImage.dir/link.txt --verbose=1
/usr/bin/c++   -O3 -DNDEBUG    CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o  -o DisplayImage -rdynamic /home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_videostab.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_videoio.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_video.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_superres.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_stitching.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_shape.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_photo.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_objdetect.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_ml.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgproc.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgcodecs.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_highgui.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_flann.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_features2d.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_core.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_calib3d.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_features2d.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_ml.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_highgui.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_videoio.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgcodecs.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_flann.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_video.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgproc.so.3.1.0 
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_core.so.3.1.0 -Wl,-rpath,
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib 
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `main':
DisplayImage.cpp:(.text.startup+0xb8): undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
DisplayImage.cpp:(.text.startup+0x1d7): undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
DisplayImage.cpp:(.text.startup+0x1f4): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
DisplayImage.cpp:(.text.startup+0x223): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
collect2: ld returned 1 exit status

From this call, I figure that the libraries in the custom location should have been used. 通过此调用,我认为应该已使用自定义位置中的库。 Why does it fail then? 为什么它失败了? I ran opencv_test_imgcodecs on both machines, which works fine. 我在两台机器上都运行过opencv_test_imgcodecs,效果很好。 Any ideas how to fix my problem? 任何想法如何解决我的问题? Is it because of the other OpenCV installation on the second system? 是因为在第二个系统上安装了其他OpenCV吗? Thank in advance for any help! 在此先感谢您的帮助!

I could solve my problem by changing the CMakeList.txt. 我可以通过更改CMakeList.txt解决问题。 I know have 我知道有

find_package( OpenCV HINTS "~/Software/OpenCVLibs/opencv-3.1.0/release/share/OpenCV/" )

in it. 在里面。 Before it was just 在那之前

find_package( OpenCV Required )

Now I also do not need to specify OpenCV_DIR any longer and can build with: 现在,我也不再需要指定OpenCV_DIR并可以使用:

cmake -DCMAKE_BUILD_TYPE=Release exampleSourceDir
make

Most of the time though I think this a more general CMake mistake. 虽然大多数时候我都认为这是更普遍的CMake错误。

I just got the same error: 我刚遇到同样的错误:

undefined reference to 'cv::imshow(cv::String const&, cv::_InputArray const&)'

And the fix that worked for me was changing my CMakeLists.txt from: 对我有用的解决方案是将我的CMakeLists.txt从以下位置更改:

add_executable(rekf
  src/rekf/rekf_node.cpp
  src/rekf/ros_rekf.cpp
  src/rekf/rekf.cpp)
add_dependencies(rekf ${catkin_EXPORTED_TARGETS})
target_link_libraries(rekf ${catkin_LIBRARIES})

To: 至:

add_executable(rekf
  src/rekf/rekf_node.cpp
  src/rekf/ros_rekf.cpp
  src/rekf/rekf.cpp)
add_dependencies(rekf ${catkin_EXPORTED_TARGETS})
target_link_libraries(rekf ${OpenCV_LIBS} ${catkin_LIBRARIES})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM