简体   繁体   中英

How to resolve opencv 'undefined reference' compilation problem, when pkg-config doesn't solve it?

I am trying to run an open-source program that uses, among other things, OpenCV. I initially ran into undefined reference errors which changed into

Package opencv was not found in the pkg-config search path

errors which then returned to undefined reference problems. The details are in the background. The compiling command and error message are included as well.

  • My initial error was a long list of 'undefined reference' errors. I could not resolve it, but noticed on, this StackOverflow question that if I tried it on Ubuntu instead of Windows, I could try adding pkg-config --cflags --libs opencv .
  • Then my compilation error changed from undefined reference to ... to Package opencv was not found in the pkg-config search path etc.
    • The accepted solution ( StackOverflow question ) seemed to be to create a file called opencv.pc, copy it to /usr/local/lib/pkgconfig , to include it in some given lines, then add some other given lines to bashrc. However, when I created the opencv.pc file, it just made a blank text file called opencv.pc instead of an actual .pc file. Furthermore, the directory /usr/local/lib/pkgconfig doesn't exist on my computer even though pkg-config is definitely installed.
  • I could not get this solution to work, so I kept searching and thought that perhaps part of the problem was that instead of installing OpenCV from the terminal, I had copied it from my Windows computer with a USB key. So I tried installing it with the terminal using this line: sudo apt-get install libopencv-dev . Now my error is once again

    undefined references

    despite the fact that I still have pkg-config ... in the compiling command. Note that it's not just OpenCV that has undefined references but also files particular to the open-source program I am trying to run (it's called 'PuRe', an eye-gaze tracking algorithm).

g++ -I /home/binny/Desktop/opencv/build/include/opencv2/imgcodecs -std=c++11 -I /home/binny/Desktop/example/ -I /home/binny/Desktop/opencv/build/include/ -I /home/binny/Desktop/winrt_x86_msvc2017/include/ -I /home/binny/Desktop/winrt_x86_msvc2017/include/QtCore /home/binny/Desktop/example/main.cpp `pkg-config --cflags --libs opencv


/tmp/ccLpYSrK.o: In function `main':
main.cpp:(.text+0x49): undefined reference to `cv::imread(cv::String const&, int)'
main.cpp:(.text+0x218): undefined reference to `PuRe::PuRe()'
main.cpp:(.text+0x238): undefined reference to `PuRe::run(cv::Mat const&, Pupil&)'
main.cpp:(.text+0x2cd): undefined reference to `cv::ellipse(cv::_InputOutputArray const&, cv::RotatedRect const&, cv::Scalar_<double> const&, int, int)'
main.cpp:(.text+0x322): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
main.cpp:(.text+0x35e): undefined reference to `PuRe::~PuRe()'
main.cpp:(.text+0x484): undefined reference to `PuRe::~PuRe()'
/tmp/ccLpYSrK.o: In function `cv::String::String(char const*)':
main.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x54): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccLpYSrK.o: In function `cv::String::~String()':
main.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccLpYSrK.o: In function `cv::String::operator=(cv::String const&)':
main.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status

From your compilation command it appears that you have installed opencv into your Desktop folder rather than into the appropriate system folders. This will work but creates problems for automatic tools like pkg-config which expect to find their files in particular places.

It may help if you set the PKG_CONFIG_PATH shell variable to include the folder containing your opencv.pc file.

You can check that this will work using

pkg-config --cflags opencv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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