使用-lopencv_videio无法进行Opencv编译



我已经安装了opencv2。

pkg-config --modversion opencv

2.4.13.5

我正在使用以下代码,

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, const char *argv[])
{
VideoCapture vid(0);
if(!vid.isOpened()){
cout<<"Camera could not load..."<<endl;
return -1;
}
namedWindow("webcam",CV_WINDOW_AUTOSIZE);
while(1){
Mat frame;
bool ctrl = vid.read(frame);
imshow("webcam",frame);
if(waitKey(0) == 27){
cout<<"The app is ended..."<<endl;
break;
}
}
return 0;
}

我使用g++ image_writer.cpp编译

/tmp/ccPWmgA0.o:在函数main': image_writer.cpp:(.text+0x38): undefined reference tocv::VideoCapture::VideoCapture(int('中image_writer.cpp:(.text+0x47(:对cv::VideoCapture::isOpened() const' image_writer.cpp:(.text+0xac): undefined reference tocv::namedWindow的未定义引用(std::__cxx11::basic_string,std::allocater>const&,int('image_writer.cpp:(.text+0xe9(:对cv::VideoCapture::read(cv::Mat&)' image_writer.cpp:(.text+0x105): undefined reference tocv::_InputArray::_Input Array(cv::Mat const&('的未定义引用image_writer.cpp:(.text+0x148(:对cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)' image_writer.cpp:(.text+0x170): undefined reference tocv::waitKey(int('的未定义引用image_writer.cpp:(.text+0x1cd(:未定义对cv::VideoCapture::~VideoCapture()' image_writer.cpp:(.text+0x254): undefined reference tocv::VideoCapture::~VideoCapture((的引用'/tmp/ccPWmgA0.o:在函数cv::Mat::~Mat()': image_writer.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference tocv::fastFree(void*('中/tmp/ccPWmgA0.o:在函数cv::Mat::release()': image_writer.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x47): undefined reference to中cv::Mat::deallocate(('collect2:错误:ld返回1退出状态

冲浪后我发现,我必须包括库文件

所以,我做了这个。g++ image_writer.cpp -lopencv_videoio

/usr/bin/ld:找不到-lopencv_videiocollect2:错误:ld返回1退出状态

我不知道如何解决这个问题。请帮我解决这个问题。任何暗示都是可以察觉的。

第1版:

pkg-config --libs opencv返回,

-L/usr/local/lib-lopencv_contrib-lopenc_stitching-lopenc_nonfree-lopencv_superes-lopenv_ocl-lopenpv_ts-loppencv_vvideostab-lopencf_gpu-lopenvc_photo-lopenccv_objectdetect-lopenncv_elegacy-lopencv_v-video-lopenc v_ml-lopencpcalib3d-lopenmv_features2d-lopencs_higgui-lopench_vimgproc-lopuncv_frann-lopencxcore-lQtCore-lQtTest-lQtGui-lQtOpenGL-lIlIl m线程-lHalf-lIex-lIlmImf-lImath-ljasper-ltiff-lpng-ljpeg-lswscale ffmpeg-lavotil ffmpeg-lavformat ffmpeg-lavcodec ffmpeg-lv4l2-lv4l1-ldc1394-lgstpbutils--0.10-lgstrif-0.10-lgstapp-0.10-lgtvideo-0.10-lxml2-llglib-2.0-llgthread-2.0-lgmodule-2.0-lgobject-2.0-lgstreamer-0.10-ldstbase-0.10-lGLU-lGL-lz-latomic-ltbb-lrt-lthread-ltdc++

OpenCV"库"实际上是多个库的集合,通常需要链接多个库。

简单的解决方案是使用pkg-config --libs opencv获取所需的所有链接器标志和库:

g++ image_writer.cpp `pkg-config --libs opencv`

使用cmake编译

cmake_minimum_required(VERSION 2.8)
project(ImageWriter)
add_executable(${PROJECT_NAME} "image_writer.cpp")    
FIND_PACKAGE(OpenCV REQUIRED )
include_directories(${OPENCV_INCLUDE_DIRS})
message(" cv_libs:     " ${OpenCV_LIBS})
message(" cv_includes: " ${OPENCV_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS})

最新更新