[英]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. 我正在尝试运行一个开源程序,其中包括OpenCV。 I initially ran into
undefined reference
errors which changed into 我最初遇到了
undefined reference
错误,它们变成了
Package opencv was not found in the pkg-config search path
在pkg-config搜索路径中找不到包opencv
errors which then returned to undefined reference
problems. 然后返回到
undefined reference
问题的错误。 The details are in the background. 细节在后台。 The compiling command and error message are included as well.
还包括编译命令和错误消息。
pkg-config --cflags --libs opencv
. pkg-config --cflags --libs opencv
。 undefined reference to ...
to Package opencv was not found in the pkg-config search path
etc. undefined reference to ...
更改undefined reference to ...
到Package opencv was not found in the pkg-config search path
等。
/usr/local/lib/pkgconfig
, to include it in some given lines, then add some other given lines to bashrc. /usr/local/lib/pkgconfig
,将其包含在某些给定的行中,然后将其他一些给定的行添加到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. opencv.pc
文件时,它只是创建了一个名为opencv.pc
的空白文本文件,而不是实际的.pc
文件。 Furthermore, the directory /usr/local/lib/pkgconfig
doesn't exist on my computer even though pkg-config
is definitely installed. pkg-config
,我的计算机上也不存在目录/usr/local/lib/pkgconfig
。 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. 我无法让这个解决方案起作用,所以我一直在搜索并认为问题的一部分可能是从终端安装OpenCV而不是从我的Windows计算机上用USB密钥复制它。 So I tried installing it with the terminal using this line:
sudo apt-get install libopencv-dev
. 所以我尝试使用这一行在终端上安装它:
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. 尽管我在编译命令中仍然有
pkg-config ...
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). 请注意,不仅OpenCV具有未定义的引用,而且还有我正在尝试运行的开源程序特有的文件(它被称为'PuRe',一种眼睛注视跟踪算法)。
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. 从编译命令看来,您已将opencv安装到Desktop文件夹中,而不是安装到相应的系统文件夹中。 This will work but creates problems for automatic tools like pkg-config which expect to find their files in particular places.
这将起作用,但会为像pkg-config这样的自动工具带来问题,这些工具希望在特定的地方找到它们的文件。
It may help if you set the PKG_CONFIG_PATH shell variable to include the folder containing your opencv.pc file. 如果将PKG_CONFIG_PATH shell变量设置为包含包含opencv.pc文件的文件夹,则可能会有所帮助。
You can check that this will work using 您可以检查这是否有效
pkg-config --cflags opencv pkg-config --cflags opencv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.