Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to cv::imread(cv::String const&, int) [duplicate]

Tags:

c++

opencv

I am trying to use OpenCV to write a simple code but I am not able to compile it successfully. Below is the code :

#include <iostream>
#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace std;
using namespace cv;

int main()
{
    Mat img = imread("insignia.jpg", -1);
    return 0;
}

and it won't work. Below is the error message I received.

/tmp/ccEMHPHa.o: In function `main':
main.cpp:(.text+0x46): undefined reference to `cv::imread(cv::String const&, int)'
/tmp/ccEMHPHa.o: In function `cv::String::String(char const*)':
main.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x58): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccEMHPHa.o: In function `cv::String::~String()':
main.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccEMHPHa.o: In function `cv::Mat::~Mat()':
main.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccEMHPHa.o: In function `cv::Mat::release()':
main.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status

I've tried searching for this issue online, but most of the answers didn't solve my problem. Can someone please help me?

like image 757
samliu Avatar asked Feb 08 '18 13:02

samliu


1 Answers

This sounds like a problem with linking. I could compile your code without problems using g++:

g++ Test.cpp -L/path/to/my/openCV/lib -lopencv_core -lopencv_imgcodecs -o Test

If you want to use further functions of Open CV you might have to link against additional Open CV libraries.

like image 103
AchimGuetlein Avatar answered Nov 08 '22 21:11

AchimGuetlein