Windows 的代码块中 OpenCV 的未定义参考错误

Undefined reference error with OpenCV in Codeblocks for Windows

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

所以我运行命令为

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

我得到很多未定义的引用 errors.I 甚至尝试 link 库作为

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

我还是一样 errors.I 使用 Opencv 2.4。5.I 没有使用 -l 选项,因为有很多库而且我不知道如何 link 所有这些库(目录) 一次。

这是编译输出。

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

可能的问题是,您在路径中使用了反斜杠。如果您使用 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

I did not use -l option since there are many libraries and I dont know how to link all those libraries(directory) at once.

对于每个必须 linked 的库,您必须使用 -l 选项。没有 GCC 命令行 选项表示 link 此目录中的任何库 。您的 -LC:\opencv\build\x86\mingw\lib 中的 -L 选项并不是那个意思。这意味着C:\opencv\build\x86\mingw\lib中搜索-l选项[=]请求的任何库29=]。但是如果你不请求任何图书馆 使用 -l 选项,然后将搜索 none。

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