简体   繁体   English

Ubuntu中OpenCV Hello World的未定义引用错误

[英]Undefined reference errors for OpenCV Hello World in Ubuntu

I'm trying the following code: 我正在尝试以下代码:

#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;

}

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

But they both give the same undefined reference errors: 但它们都给出了相同的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()'

How can I fix this? 我怎样才能解决这个问题?

If pkg-config opencv --cflags --libs command finds OpenCV include files and libraries below compilation works wituout any errors. 如果pkg-config opencv --cflags --libs命令找到OpenCV包含文件,并且编译工作以下的库可以解决任何错误。

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM