由于未定义的引用,无法获取OpenCV进行编译?

Posted hello-pdg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了由于未定义的引用,无法获取OpenCV进行编译?相关的知识,希望对你有一定的参考价值。

错误提示:

CMakeFiles/my_subscriber.dir/src/my_subscriber.cpp.o:在函数‘imageCallback(boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&)’中:
my_subscriber.cpp:(.text+0xb0):对‘cv::imshow(cv::String const&, cv::_InputArray const&)’未定义的引用
CMakeFiles/my_subscriber.dir/src/my_subscriber.cpp.o:在函数‘main’中:
my_subscriber.cpp:(.text+0x405):对‘cv::namedWindow(cv::String const&, int)’未定义的引用
my_subscriber.cpp:(.text+0x419):对‘cv::startWindowThread()’未定义的引用
my_subscriber.cpp:(.text+0x66b):对‘cv::destroyWindow(cv::String const&)’未定义的引用
collect2: error: ld returned 1 exit status
my_image_transport/CMakeFiles/my_subscriber.dir/build.make:128: recipe for target ‘/home/pdg/catkin_ws/devel/lib/my_image_transport/my_subscriber‘ failed
make[2]: *** [/home/pdg/catkin_ws/devel/lib/my_image_transport/my_subscriber] Error 1
CMakeFiles/Makefile2:1706: recipe for target ‘my_image_transport/CMakeFiles/my_subscriber.dir/all‘ failed
make[1]: *** [my_image_transport/CMakeFiles/my_subscriber.dir/all] Error 2
Makefile:138: recipe for target ‘all‘ failed
make: *** [all] Error 2

解决方法:

CMakeLists.txt中要有target_link_libraries(my_subscriber ${catkin_LIBRARIES} ${OpenCV_LIBS})

 

完整CMakeLists.txt文件如下:

cmake_minimum_required(VERSION 2.8.3)

project(my_image_transport)

find_package(catkin REQUIRED COMPONENTS

      OpenCV

      cv_bridge

      image_transport

)

 

 

include_directories(

 include

   ${catkin_INCLUDE_DIRS}

   ${OpenCV_INCLUDE_DIRS}

)

 

 

add_executable(my_subscriber src/my_subscriber.cpp)
target_link_libraries(my_subscriber ${catkin_LIBRARIES} ${OpenCV_LIBS})

以上是关于由于未定义的引用,无法获取OpenCV进行编译?的主要内容,如果未能解决你的问题,请参考以下文章

带有devc ++的opencv中未定义的引用[重复]

未定义的符号引用,但库已链接

链接时未定义的引用 - 缺少库?

opencv tesseract 未定义的引用错误

对 `JNI_CreateJavaVM' linux 的未定义引用

即使在链接库时,也会获得对 OpenCV 元素的未定义引用。使用 CMake