【问题标题】:Undefined reference error with OpenCV in Codeblocks for Windows适用于 Windows 的代码块中的 OpenCV 未定义引用错误
【发布时间】:2016-07-06 01:14:31
【问题描述】:

我已经为代码块安装了 Opencv,并且程序在代码块中运行良好。但是当我尝试从命令提示符运行程序时,我得到一个错误fatal error:opencv2/opencv.hpp No such file or directory compilation terminated

所以我将命令运行为

g++ -IC:\opencv\build\include -o main.exe main.cpp

我得到许多未定义的引用错误。我什至尝试将库链接为

g++ -IC:\opencv\build\include -LC:\opencv\build\x86\mingw\lib -o main.exe main.cpp

我仍然遇到同样的错误。我使用的是 Opencv 2.4.5。我没有使用 -l 选项,因为有很多库,我不知道如何一次链接所有这些库(目录)。

这是编译输出。

C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x6d): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0xb7): undefined reference to `cv::Mat::convertTo(cv::_OutputArray const&, int, double, double) const'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x152): undefined reference to `cv::Mat::zeros(int, int, int)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x322): undefined reference to `cv::noArray()'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x384): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x3bd): undefined reference to `cv::meanStdDev(cv::_InputArray const&, cv::_OutputArray const&, cv::_OutputArray const&, cv::_InputArray const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x5c7): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x5ea): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x645): undefined reference to `cv::resize(cv::_InputArray const&, cv::_OutputArray const&, cv::Size_<int>, double, double, int)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x665): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x685): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x6d6): undefined reference to `cv::threshold(cv::_InputArray const&, cv::_OutputArray const&, double, double, int)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x705): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x752): undefined reference to `cv::Mat::convertTo(cv::_OutputArray const&, int, double, double) const'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x775): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x7c2): undefined reference to `cv::Mat::convertTo(cv::_OutputArray const&, int, double, double) const'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x7e5): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x808): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x828): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x86e): undefined reference to `cv::inpaint(cv::_InputArray const&, cv::_InputArray const&, cv::_OutputArray const&, double, int)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x89e): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x8be): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x910): undefined reference to `cv::resize(cv::_InputArray const&, cv::_OutputArray const&, cv::Size_<int>, double, double, int)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x92a): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x971): undefined reference to `cv::Mat::convertTo(cv::_OutputArray const&, int, double, double) const'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0xc68): undefined reference to `cv::namedWindow(std::string const&, int)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0xcee): undefined reference to `cv::imread(std::string const&, int)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0xd3f): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0xd8c): undefined reference to `cv::Mat::convertTo(cv::_OutputArray const&, int, double, double) const'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0xe05): undefined reference to `cv::operator-(cv::Scalar_<double> const&, cv::Mat const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0xe6b): undefined reference to `cv::operator+(cv::Mat const&, cv::Mat const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0xebb): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0xede): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0xf2f): undefined reference to `cv::threshold(cv::_InputArray const&, cv::_OutputArray const&, double, double, int)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0xf74): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0xf97): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0xff2): undefined reference to `cv::resize(cv::_InputArray const&, cv::_OutputArray const&, cv::Size_<int>, double, double, int)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x103d): undefined reference to `cv::operator*(cv::Mat const&, double)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x105d): undefined reference to `cv::_InputArray::_InputArray(cv::MatExpr const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x10b8): undefined reference to `cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text+0x1110): undefined reference to `cv::waitKey(int)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]+0x2d): undefined reference to `cv::fastFree(void*)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text$_ZN2cv3MataSERKS0_[__ZN2cv3MataSERKS0_]+0x102): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text$_ZN2cv3Mat7releaseEv[__ZN2cv3Mat7releaseEv]+0x3e): undefined reference to `cv::Mat::deallocate()'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text$_ZNK2cv3MatclENS_5RangeES1_[__ZNK2cv3MatclENS_5RangeES1_]+0x23): undefined reference to `cv::Mat::Mat(cv::Mat const&, cv::Range const&, cv::Range const&)'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text$_ZN2cv12_OutputArrayC1IdLi4ELi1EEERNS_4MatxIT_XT0_EXT1_EEE[__ZN2cv12_OutputArrayC1IdLi4ELi1EEERNS_4MatxIT_XT0_EXT1_EEE]+0x21): undefined reference to `vtable for cv::_OutputArray'
C:\Users\HP\AppData\Local\Temp\ccraot3b.o:main.cpp:(.text$_ZN2cv11_InputArrayC2IdLi4ELi1EEERKNS_4MatxIT_XT0_EXT1_EEE[__ZN2cv11_InputArrayC2IdLi4ELi1EEERKNS_4MatxIT_XT0_EXT1_EEE]+0xe): undefined reference to `vtable for cv::_InputArray'
collect2.exe: error: ld returned 1 exit status

【问题讨论】:

  • 请将编译输出添加到问题中!

标签: c++ opencv gcc cmd undefined-reference


【解决方案1】:

可能的问题是,您在路径中使用了反斜杠。如果您使用 MinGW 或 Cygwin(我想您是)进行编译,那么在该 shell 中,反斜杠是一个转义字符。所以如果你想要简单的反斜杠,你通常必须转义它:\\。但是,这不是解决您的问题的正确方法。您应该在路径中使用正斜杠/

g++ -IC:/opencv/build/include -o main.exe main.cpp

g++ -IC:/opencv/build/include -LC:/opencv/build/x86/mingw/lib -o main.exe main.cpp

【讨论】:

  • 普通斜线不起作用。我已经添加了编译输出。
【解决方案2】:

我没有使用 -l 选项,因为有很多库,我不知道如何一次链接所有这些库(目录)。

您必须为每个必须链接的库使用-l 选项。没有 GCC 命令行 选项表示链接此目录中的任何库-L 选项中的 -LC:\opencv\build\x86\mingw\lib 并不意味着这一点。这意味着搜索 C:\opencv\build\x86\mingw\lib -l 选项 请求的任何库。但是如果你不请求任何库 使用 -l 选项,则不会搜索任何内容。

当您在 Code::Blocks 中构建程序时,执行的编译和链接命令会打印在 构建日志 选项卡中(不是 构建消息 标签)。因此,如果您查看构建日志,您将确切看到必须将哪些--options 传递给编译命令,然后传递给链接命令。在命令提示符下以相同的顺序使用完全相同的选项,您将以完全相同的方式构建程序。

【讨论】:

  • @sai 要使用大量命令和长命令行构建复杂的软件,我们通常使用称为make 的更高级别的命令行工具。这是tutorial about GCC and Make 你会发现make 的Windows 端口随你的mingw GCC 发行版一起提供。它被称为mingw32-make.exe。 Code::Blocks 在内部使用make 来构建您的项目。如今,称为CMake 的更高级别的工具也非常流行。 CMake也用make,所以先了解make比较好。
  • 好的,我会调查的。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
  • 2015-02-25
  • 2012-04-23
  • 1970-01-01
  • 2017-05-10
相关资源
最近更新 更多