OpenCV:使用cmake

时间:2016-03-11 11:10:39

标签: c++ linux opencv gcc dynamic-linking

我必须在两个系统上使用OpenCV,这两个系统都是Linux。我的设置适用于第一个系统,但第二个系统失败。对于这两个系统,我使用以下方法在自定义位置安装了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

和调试类似。目录是不同的。一个是我的笔记本电脑上的用户,另一个是多用户系统上的不同用户。

然后我使用OpenCV文档中给出的示例来测试我的设置: http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html

使用我使用的自定义OpenCV位置构建示例

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

这适用于第一个系统(gcc版本5.3.0),但在第二个系统(gcc版本4.6.3)上失败。不要认为gcc版本在这里很重要,但要说明它们的完整性。我认为它可能与第一个系统上的安装系统路径中没有OpenCV有关,而在第二个系统中则有。但是构建失败了:

/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

通过此调用,我认为应该使用自定义位置中的库。那为什么失败呢?我在两台机器上都运行了opencv_test_imgcodecs,效果很好。 任何想法如何解决我的问题?是因为第二个系统上的其他OpenCV安装? 提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我可以通过更改CMakeList.txt来解决我的问题。我知道有

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

在里面。在此之前

find_package( OpenCV Required )

现在我也不需要再指定OpenCV_DIR了,可以使用:

构建
cmake -DCMAKE_BUILD_TYPE=Release exampleSourceDir
make

答案 1 :(得分:0)

大多数时候我认为这是一个更普遍的CMake错误。

我得到了同样的错误:

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

对我有用的解决方法是改变我的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})

要:

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})