【问题标题】:Opencv compilation is not working using -lopencv_videoioOpencv 编译无法使用 -lopencv_videoio
【发布时间】:2023-04-02 17:38:01
【问题描述】:

我已经安装了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::allocator > const&, int)' image_writer.cpp:(.text+0xe9): 未定义引用cv::VideoCapture::read(cv::Mat&)' image_writer.cpp:(.text+0x105): undefined reference tocv::_InputArray::_InputArray(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 tocv::Mat::deallocate()' collect2:错误:ld 返回 1 个退出状态

我上网后发现,我必须包含lib文件

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

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

我不知道如何解决这个问题。请帮我解决这个问题。任何提示都会很明显。

EDIT-1:

pkg-config --libs opencv 返回,

-L/usr/local/lib -lopencv_contrib -lopencv_stitching -lopencv_nonfree -lopencv_superres -lopencv_ocl -lopencv_ts -lopencv_videostab -lopencv_gpu -lopencv_photo -lopencv_objdetect -lopencv_legacy -lopencv_calib3d -lopencv_features2d -lopencv_highguil lQtCore -lQtTest -lQtGui -lQtOpenGL -lIlmThread -lHalf -lIex -lIlmImf -lImath -ljasper -ltiff -lpng -ljpeg -lswscale-ffmpeg -lavutil-ffmpeg -lavformat-ffmpeg -lavcodec-ffmpeg -lv4l2 -lv4l1 -ldc1394 -lgstpbutils- 0.10 -lgstriff-0.10 -lgstapp-0.10 -lgstvideo-0.10 -lxml2 -lglib-2.0 -lgthread-2.0 -lgmodule-2.0 -lgobject-2.0 -lgstreamer-0.10 -lgstbase-0.10 -lGLU -lGL -lz -latomic -ltbb - lrt -lpthread -lm -ldl -lstdc++

【问题讨论】:

  • 您可以使用pkg-config --libs opencv列出所需的链接器标志和库。
  • @Someprogrammerdude- 请查看更新后的帖子

标签: c++ c opencv c++11


【解决方案1】:

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

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

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

【讨论】:

  • 太棒了!它已编译。你能告诉我如何在cmake中做同样的事情吗?即,如何在 Cmakelists.txt 文件中添加它?
  • @MohamedThasinah 搜索cmake opencv 时出现this link 作为我的第一个命中。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2013-03-06
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2015-07-23
    相关资源
    最近更新 更多