OpenCV 2.3编译问题 - 未定义的反射 - Ubuntu 11.10

use*_*414 38 c++ ubuntu opencv g++

在此先感谢任何帮助......

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

我正在尝试在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;
}
Run Code Online (Sandbox Code Playgroud)

但是rrrrrrrrrrrrr,我收到这些错误信息......

-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
Run Code Online (Sandbox Code Playgroud)

Emp*_*ian 73

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

pkg-config opencv --libs
Run Code Online (Sandbox Code Playgroud)

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

尝试

g++ -o test_1 test_1.cpp `pkg-config opencv --cflags --libs` 
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!我只是盯着这一个小时,这就是答案. (3认同)
  • 这么多,我有同样的问题,甚至相同的操作系统和版本. (2认同)