【问题标题】:OpenCV 2.3 Compiling Issue - Undefined Refence - Ubuntu 11.10OpenCV 2.3 编译问题 - 未定义的引用 - Ubuntu 11.10
【发布时间】:2011-12-10 14:58:24
【问题描述】:

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

【问题讨论】:

    标签: c++ ubuntu opencv g++


    【解决方案1】:

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

    pkg-config opencv --libs
    

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

    试试

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

    【讨论】:

    • 谢谢!我只是盯着这个看了一个小时,这就是答案。
    • 非常感谢,我遇到了同样的问题,甚至相同的操作系统和版本。
    • 还是有同样的问题
    • 我在 Ubuntu 16.04 和 opencv 3 .4 上遇到了同样的问题,这解决了我的问题
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

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

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

      另见CMake error configuring opencv

      【讨论】:

        【解决方案4】:

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

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

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

        【讨论】:

          猜你喜欢
          • 2012-01-10
          • 2017-08-22
          • 2011-12-08
          • 2012-03-07
          • 2019-06-23
          • 2021-03-14
          • 1970-01-01
          • 2011-12-15
          • 1970-01-01
          相关资源
          最近更新 更多