用mingw编译opencv3.1

Posted

技术标签:

【中文标题】用mingw编译opencv3.1【英文标题】:Compile opencv3.1 with mingw 【发布时间】:2016-05-26 10:07:54 【问题描述】:

我想用mingw-w64编译下面的代码。

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

int main(int argc, char** argv)

  Mat im = imread("lena.jpg", 1);
  if (im.empty())
  
    cout << "Cannot open image!" << endl;
    return -1;
  

  imshow("image", im);
  waitKey(0);

  return 0;

关注Getting started with OpenCV 2.4 and MinGW on Windows 7之后

我用g++ -I D:\opencv\build\include -L D:\opencv\build\x64\vc14\lib -lopencv_world310 .\loadimg.cpp编译代码

但它返回未定义的引用

C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text+0x40): undefined reference to `cv::imread(cv::String const&, int)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text+0xb7): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text+0xd9): undefined reference to `cv::waitKey(int)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text$_ZN2cv6StringC1EPKc[_ZN2cv6StringC1EPKc]+0x4a): undefined reference to `cv::String::allocate(unsigned long long)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text$_ZN2cv6StringD1Ev[_ZN2cv6StringD1Ev]+0x11): undefined reference to `cv::String::deallocate()'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text$_ZN2cv3MatD1Ev[_ZN2cv3MatD1Ev]+0x36): undefined reference to `cv::fastFree(void*)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text$_ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x48): undefined reference to `cv::Mat::deallocate()'

库是opencv3.1,只有一个库opencv_world310

有什么建议吗?

谢谢。

【问题讨论】:

【参考方案1】:
g++ -I D:\opencv\build\include -L D:\opencv\build\x64\vc14\lib -lopencv_world310 .\loadimg.cpp

错了。

g++ -I D:\opencv\build\include -L D:\opencv\build\x64\vc14\lib .\loadimg.cpp -lopencv_world310 

是对的。解释here

【讨论】:

以上是关于用mingw编译opencv3.1的主要内容,如果未能解决你的问题,请参考以下文章

用mingw编译Ogitor

代码可以用 VS 编译,但不能用 MinGW

Cygwin 和MinGW 的区别与联系是怎样的

MinGw Gcc / Gtk+3 / Cmake 编译程序. 如何去掉控制台窗口?

编译wxWidgets —— windowsvc71bcc32MinGW与命令行

QT用VS还是MINGW编译好,有啥区别