04中编译和链接OpenCV

04中编译和链接OpenCV

本文介绍了在Ubuntu 12.04中编译和链接OpenCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚花了一段时间让openCV在Ubuntu 12.04中正确链接,并认为我会分享我为了别人的利益而学到的东西。

可以在Ubuntu存储库中找到:

  sudo apt-get install libopencv-dev 

这很好,但我相信(如果我错了,请更正我)此版本的opencv对这些库有不同的命名约定。主要区别在于,在c ++中include行应该是

  #includeopencv2 / opencv.hpp

这会让您的代码编译为对象但不能链接。另一个区别是静态库也已经从 libcv * 重命名为 libopencv * 。例如,二进制文件现在可以位于

  /usr/lib/libopencv_core.so 
/ usr / lib / libopencv_highgui .so



为了解决这个问题,我需要通过更改我的编译器命令来明确告诉链接器有关新库名称的信息

  g ++ main.cpp -lopencv_core -lopencv_highgui ... 
$ b $或者在CMake中
$ b $ pre $ lt; code> target_link_libraries(main opencv_core opencv_highgui ...)

我希望这会有所帮助。如果有人知道比我更多,我很想知道这里发生了什么。



-Mike

解决方案

个人而言,我使用'pkg-config'来获取编译标志。

  g ++`pkg-config --cflags opencv` main.c`pkg-config --libs opencv` -o main 


$

  #include< stdio.h> 
#include< cv.h>

int main(void)
{
printf(%s\r\,CV_VERSION);
printf(%u。%u。%u \ r \ n,CV_MAJOR_VERSION,CV_MINOR_VERSION,CV_SUBMINOR_VERSION);
}


I just spent a frustratingly long time getting openCV to link properly in Ubuntu 12.04 and thought I would share what I learned for the benefit of others.

OpenCV is now available in the Ubuntu repositories as

sudo apt-get install libopencv-dev

which is great, but I believe (please correct me if I'm wrong) that this version of opencv has a different naming convention for the libraries. The main difference is that in c++ the include line should read

#include "opencv2/opencv.hpp"

That will get your code compiling to object but not linking. The other difference is that the static libraries have also been renamed from libcv* to libopencv*. For example binaries can now be located at

/usr/lib/libopencv_core.so
/usr/lib/libopencv_highgui.so
.
.
.

To fix this I needed to explicitly tell the linker about the new library names by changing my compiler command to

g++ main.cpp -lopencv_core -lopencv_highgui ...

Or in CMake

target_link_libraries(main opencv_core opencv_highgui ...)

I hope this helps. And if anyone knows more than me I'd love to find out what's going on here.

-Mike

解决方案

Personally, I'm using 'pkg-config' to get the compilation flags.

g++ `pkg-config --cflags opencv` main.c `pkg-config --libs opencv` -o main

Example of main:

#include <stdio.h>
#include <cv.h>

int main(void)
{
    printf("%s\r\n", CV_VERSION);
    printf("%u.%u.%u\r\n", CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_SUBMINOR_VERSION);
}

这篇关于在Ubuntu 12.04中编译和链接OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:38