undefined reference to `cv::VideoCapture::VideoCapture()

最近基于TX2平台做项目,需要用到opencv的VideoCapture功能,我在使用过程中遇到了标题中所描述的问题,在此记录下解决的方法。

  • cmakelist添加目标链接库
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  eigen_conversions
  message_generation
  cv_bridge
  image_transport
)

add_executable(rtsp_camera
                src/network/rtsp_camera.cpp)

 target_link_libraries(rtsp_camera
   ${OpenCV_LIBRARIES}
   ${catkin_LIBRARIES}
 ) 
  •  .cpp文件中添加头文件
#include 

#include 
#include 
#include 

#include 
#include 
#include 
  • .cpp文件中相关代码展示
void 
RtspCamera::spin()
{
    ros::Rate loop_rate(3);

    //rtsp视频流地址
    string filename = "rtsp://admin:[email protected]:554/cam/realmonitor?channel=1&subtype=0";
    Mat frame;
    cv::VideoCapture cap;
    cap.open(filename);

    if (!cap.isOpened()) {
        cerr << "打开视频流失败\n";
        return;
    }    

    while(ros::ok())
    {
        cap.read(frame);
        if (frame.empty()) {
            cerr << "未提取到视频帧\n";
            break;
        }

        image_ = cv_bridge::CvImage(std_msgs::Header(), "rgb8", frame).toImageMsg();
        image_raw_pub_.publish(&image_);

        ros::spinOnce();
        loop_rate.sleep();
    }
}
  • 编译发现报标题中所示错误,查看/usr/local/include/opencv2/videoio.hpp中有VideoCapture类,因此猜想可能没有找到相关静态链接库,因此在cmakelists文件中补充相关静态链接库,如下所示,问题解决
 target_link_libraries(rtsp_camera
   ${OpenCV_LIBRARIES}
   ${catkin_LIBRARIES}
   /usr/local/lib/libopencv_videoio.so
 ) 

你可能感兴趣的:(c++)