Ubuntu中OpenCV Hello World的未定义参考错误

时间:2019-06-10 23:57:36

标签: c++ opencv g++

我正在尝试以下代码:

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main(int argc, char** argv) {
    namedWindow("Output",1);
    Mat output = Mat::zeros( 120, 350, CV_8UC3 );
    putText(output,"Hello World",cvPoint(15,70),
            FONT_HERSHEY_PLAIN,3,cvScalar(0,255,0),4);
    imshow("Output", output);
    waitKey(0);
    return 0;

}

然后我尝试了g++ -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui opencv_hello.cpp -o opencv_hello g++ pkg-config opencv cvblob --cflags --libs opencv_hello.cpp -o opencv_hello

但是它们都给出相同的undefined reference错误:

opencv_hello.cpp:(.text+0x132): undefined reference to `cv::namedWindow(cv::String const&, int)'
opencv_hello.cpp:(.text+0x15f): undefined reference to `cv::Mat::zeros(int, int, int)'
opencv_hello.cpp:(.text+0x26f): undefined reference to `cv::putText(cv::_InputOutputArray const&, cv::String const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)'
opencv_hello.cpp:(.text+0x2d7): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
opencv_hello.cpp:(.text+0x2ff): undefined reference to `cv::waitKey(int)'
/tmp/cctt8VGQ.o: In function `cv::String::String(char const*)':
opencv_hello.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/cctt8VGQ.o: In function `cv::String::~String()':
opencv_hello.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/cctt8VGQ.o: In function `cv::Mat::~Mat()':
opencv_hello.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/cctt8VGQ.o: In function `cv::Mat::release()':
opencv_hello.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'

我该如何解决?

1 个答案:

答案 0 :(得分:0)

如果pkg-config opencv --cflags --libs命令在编译下发现OpenCV包含文件和库,则不会出现任何错误。

    g++ opencv_hello.cpp -o opencv_hello $(pkg-config opencv --cflags --libs)

    g++ opencv_hello.cpp -o opencv_hello `pkg-config opencv --cflags --libs`