C++ OpenCV 2.3 编译问题 - 未定义参考 - Ubuntu 11.10

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7816607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:32:17  来源:igfitidea点击:

OpenCV 2.3 Compiling Issue - Undefined Refence - Ubuntu 11.10

c++ubuntuopencvg++

提问by user1002414

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

系统信息:Ubuntu 11.10(64 位)和 OpenCV 2.3(今天安装)

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

我正在尝试在 OpenCV 2.3 中编译一些非常简单的代码,但出现了一个奇怪的错误。

#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

回答by Employed Russian

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.cppin this case): the order of sources and libraries on the link line matters.

是档案库。将存档库放在需要它们的源之前是不正确的(test_1.cpp在这种情况下):链接线上源和库的顺序很重要

Try

尝试

g++ -o test_1 test_1.cpp `pkg-config opencv --cflags --libs` 

回答by dividebyzero

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?

我遇到了同样的问题,但我发现 pkg-config opencv --cflags 正在打印“-I/usr/include/opencv”而不是“-I/usr/include/opencv2”......也许是一个包错误乌班图?

回答by Tim Kuipers

I am using cmake and had similar problems.

我正在使用 cmake 并且遇到了类似的问题。

Something weird is going on with the cmake configuration files.

cmake 配置文件发生了一些奇怪的事情。

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

对我来说,问题是通过简单地将 OPENCV_FOUND 设置为 TRUE 并将 OpenCV_FOUND 设置为 TRUE 来解决的。

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

我还必须将 OpenCV_DIR 设置为 /usr/local/share/OpenCV。

See also CMake error configuring opencv

另请参阅配置 opencv 的 CMake 错误

回答by IEEE754

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

@EmployedRussian 的回答对我也有用。对于那些想知道如何在 Eclipse 中指定此命令的人,请使用这篇文章 -

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

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

不加gtk+,改用opencv;不是将新标志添加到“杂项链接器标志”,而是在 ${INPUT} 之后的末尾添加新标志 - Project->Right click->Properties->C/C++ Build ->Settings->GCC C++ linker- >专家设置:命令行模式