Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling and Linking OpenCV in Ubuntu 12.04

Tags:

gcc

opencv

ubuntu

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

like image 677
Mike Ounsworth Avatar asked Dec 16 '12 18:12

Mike Ounsworth


1 Answers

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);
}
like image 189
ssinfod Avatar answered Oct 22 '22 17:10

ssinfod