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

发布于 2024-12-10 19:38:59 字数 1573 浏览 0 评论 0原文

系统信息: 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

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

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

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

睫毛溺水了 2024-12-17 19:38:59

我猜测输出中至少有一些库

pkg-config opencv --libs

是存档库。将存档库放在需要它们的源之前是不正确的(在本例中为 test_1.cpp):链接行上源和库的顺序 很重要

尝试

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

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

Try

g++ -o test_1 test_1.cpp `pkg-config opencv --cflags --libs` 
旧时浪漫 2024-12-17 19:38:59

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

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?

骄兵必败 2024-12-17 19:38:59

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

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

对我来说,只需将 OPENCV_FOUND 设置为 TRUE 并将 OpenCV_FOUND 设置为 TRUE 即可解决问题。

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

另请参阅CMake 配置 opencv 时出错

I am using cmake and had similar problems.

Something weird is going on with the cmake configuration files.

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

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

See also CMake error configuring opencv

爱给你人给你 2024-12-17 19:38:59

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

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

不用添加gtk+,而是使用opencv;
不要将新标志添加到“杂项链接器标志”,而是在 ${INPUT} 之后的末尾添加新标志 -
项目->右键->属性->C/C++ Build->设置->GCC C++链接器->专家设置:命令行模式

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文