OpenCV 2.3 Compiling Issue - Undefined Refence - Ubuntu 11.10
Asked Answered
C

4

38

System Info: Ubuntu 11.10 (64 bits) with OpenCV 2.3 (installed today)

I'm trying to compile some very simple code in OpenCV 2.3 but I'm getting a weird error.

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

int main(){
  cv::Mat image=cv::imread("img.jpg");
  cv::namedWindow("My Image");
  cv::imshow("My Image",image);
  cv::waitKey(0);
  return 1;
}

however, I'm getting these error messages...

-SG41:~/Desktop$ g++ `pkg-config opencv --cflags --libs` -o test_1 test_1.cpp 
/tmp/ccCvS1ys.o: In function `main':
test_1.cpp:(.text+0x44): undefined reference to `cv::imread(std::basic_string<char,    std::char_traits<char>, std::allocator<char> > const&, int)'
test_1.cpp:(.text+0x8e): undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
test_1.cpp:(.text+0xbc): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
test_1.cpp:(.text+0xf0): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
test_1.cpp:(.text+0x112): undefined reference to `cv::waitKey(int)'
/tmp/ccCvS1ys.o: In function `cv::Mat::~Mat()':
test_1.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccCvS1ys.o: In function `cv::Mat::release()':
test_1.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
Chimere answered 19/10, 2011 at 4:40 Comment(0)
D
73

I am guessing that at least some of the libraries in the output of

pkg-config opencv --libs

are archive libraries. It is incorrect to put archive libraries before sources that need them (test_1.cpp in this case): the order of sources and libraries on the link line matters.

Try

g++ -o test_1 test_1.cpp `pkg-config opencv --cflags --libs` 
Danieldaniela answered 19/10, 2011 at 4:53 Comment(4)
Thanks! I just stared at this for an hour, and this was the answer.Sibylsibylla
Thx so much, I had the very same issue, even same OS and version.Coranto
still having the same problemKen
I had the same issue on Ubuntu 16.04 and opencv 3 .4 and this solved the problem for meJamiejamieson
S
2

I was having the same problem, but I found out pkg-config opencv --cflags is printing "-I/usr/include/opencv" instead of "-I/usr/include/opencv2"... Maybe a package bug on Ubuntu?

Singlehanded answered 29/8, 2013 at 2:26 Comment(0)
S
0

I am using cmake and had similar problems.

Something weird is going on with the cmake configuration files.

For me the problems were solved by simply setting OPENCV_FOUND to TRUE and OpenCV_FOUND to TRUE.

Also I had to set OpenCV_DIR to /usr/local/share/OpenCV.

See also CMake error configuring opencv

Spartan answered 5/10, 2015 at 12:57 Comment(0)
H
0

@EmployedRussian 's answer worked for me too. For those who are wondering how to specify this command in Eclipse, use this post -

https://www.eclipse.org/forums/index.php?t=msg&goto=233377&

Instead of adding gtk+, use opencv; Instead of adding the new flags to 'Miscellaneous linker flags', add the new flags at the end after ${INPUT} in - Project->Right click->Properties->C/C++ Build ->Settings->GCC C++ linker->Expert Settings: command line pattern

Hogan answered 12/10, 2015 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.