编译期间无法链接opencv库

时间:2018-09-25 16:00:54

标签: c++ opencv mingw

我按照this tutorial从github源码构建了opencv,一切都成功了,但是编译了示例程序以画一个圆

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;

int main( )
{    
  // Create black empty images
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );

  // Draw a line 
  line( image, Point( 15, 20 ), Point( 70, 50), Scalar( 110, 220, 0 ),  2, 8 );
  imshow("Image",image);

  waitKey( 0 );
  return(0);
}

我遇到错误。根据{{​​3}},我必须在编译程序时链接导入的库。但是在我的MinGW/lib文件夹中没有opencv_core.aopencv_highgui.a这样的文件,相反,我有libopencv_core400.dll.alibopencv_highgui400.dll.a

this question

因此,如果我尝试使用选项-lopencv_core-lopencv_highgui

D:\Programs\CppProgramming>g++ -lopencv_core -lopencv_highgui -std=c++11 OpenCVTest.cpp
C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/ld.exe: cannot find -lopencv_core
C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/ld.exe: cannot find -lopencv_highgui
collect2.exe: error: ld returned 1 exit status

,甚至带有选项-llibopencv_core400和-llibopencv_highgui400

D:\Programs\CppProgramming>g++ -llibopencv_core400 -llibopencv_highgui400 -std=c++11 OpenCVTest.cpp
C:\Users\vivek\AppData\Local\Temp\ccnjHIL5.o:OpenCVTest.cpp:(.text+0x80): undefined reference to `cv::Mat::zeros(int, int, int)'
C:\Users\vivek\AppData\Local\Temp\ccnjHIL5.o:OpenCVTest.cpp:(.text+0x157): undefined reference to `cv::circle(cv::_InputOutputArray const&, cv::Point_<int>, int, cv::Scalar_<double> const&, int, int, int)'
C:\Users\vivek\AppData\Local\Temp\ccnjHIL5.o:OpenCVTest.cpp:(.text+0x1c2): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
C:\Users\vivek\AppData\Local\Temp\ccnjHIL5.o:OpenCVTest.cpp:(.text+0x1f6): undefined reference to `cv::waitKey(int)'
C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/ld.exe: C:\Users\vivek\AppData\Local\Temp\ccnjHIL5.o: bad reloc address 0x12 in section `.text$_ZN2cv11_InputArrayC2Ev[__ZN2cv11_InputArrayC2Ev]'
C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status

我不确定我要去哪里。在哪里可以找到那些可以成功链接和编译程序的库?任何帮助,将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:0)

如果要自定义标头或库并在代码块上运行,则需要在编译器中添加链接器。尝试添加所有lib文件 settings-> compiler-> linker setting

我认为这可以解决您的问题。

https://i.stack.imgur.com/xPVrO.png

答案 1 :(得分:0)

按以下顺序使用以下命令解决了我的编译问题

g++ -std=c++11 OpenCVTest.cpp -llibopencv_core400 -llibopencv_highgui400 -llibopencv_imgproc400