简体   繁体   English

如何解决opencv'undefined reference'编译问题,当pkg-config无法解决时?

[英]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. 还包括编译命令和错误消息。

  • 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 . 我无法解决它,但注意到,这个StackOverflow问题 ,如果我在Ubuntu而不是Windows上尝试它,我可以尝试添加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. 然后我的编译错误从未undefined reference to ...更改undefined reference to ...Package opencv was not found in the pkg-config search path等。
    • 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. 接受的解决方案( StackOverflow问题 )似乎是创建一个名为opencv.pc的文件,将其复制到/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.

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