OpenCV 2.3编译问题 - 未定义的引用 - Ubuntu 11.10

38

系统信息: Ubuntu 11.10(64位), 安装了今天的OpenCV 2.3版本。

我正在尝试编译一些非常简单的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;
}

不过,我收到了这些错误消息...

-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
4个回答

73

我猜输出结果中至少有一些库是

pkg-config opencv --libs

这些是存档库。在链接顺序中,把需要它们的源文件(test_1.cpp)放在存档库之前是不正确的,因为源文件和库文件的顺序很重要

尝试:

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

3
谢谢!我刚刚盯着这个问题看了一个小时,结果就是这个。 - nont
2
非常感谢,我也遇到了完全相同的问题,甚至是相同的操作系统和版本。 - Trass3r
仍然存在相同的问题。 - Gucci Koo
我在Ubuntu 16.04和opencv 3.4上遇到了同样的问题,这个方法解决了我的问题。 - Sam Radhakrishnan

2

我曾经遇到相同的问题,但是我发现pkg-config opencv --cflags输出的是"-I/usr/include/opencv"而不是"-I/usr/include/opencv2"......这可能是Ubuntu上的一个软件包错误吗?


0

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

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

不要添加gtk+,使用opencv; 不要将新标记添加到“Miscellaneous linker flags”,请在${INPUT}后面以-Project->右键单击->属性->C/C++ Build ->设置->GCC C++链接器->专家设置:命令行模式


0

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

cmake配置文件出现了一些奇怪的问题。

对我来说,问题通过将OPENCV_FOUND和OpenCV_FOUND设置为TRUE来解决。

此外,我还不得不将OpenCV_DIR设置为/usr/local/share/OpenCV。

还可以参考CMake error configuring opencv


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接