Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ubuntu, opencv and linking

I installed Ubuntu a few days ago, and used apt-get to install build_essentials, opencv, highgui, etc. g++ is 4.6.1, opencv appears to be 2.1.0.. I didn't build opencv from source..

We have some software that uses opencv functionality. Let's pretend the source files are named a.cpp and b.cpp. I compile to a.o and b.o and then put those into a .so library (call it libab.so).

Finally, there's a file with main in it (call it z.cpp). I try to build an executable from it, but I get a ton of "undefined reference" errors to cv:: stuff. My link line looks something like this:

g++ -fPIC -g z.cpp -L../lib -lab -lml -lcvaux -lhighgui -lcv -lcxcore -o z.out

then I get the undefined reference errors (all of which are to cv:: stuff).

The interesting part is if I link directly with my .o files, it builds just fine. So this:

g++ -fPIC -g z.cpp a.o b.o -lml -lcvaux -lhighgui -lcv -lcxcore -o z.out

works.

Everything I've read seems to imply this is likely a link-line-ordering issue, but I have tried all ordering permutations and get the same problem, so I really don't think its my issue, but I could still be wrong. Anyone have any ideas how I can get this to build with my library, and why it would act differently if I build with the complete set of .o files that are in the library successfully, but can't build with the library itself?

like image 974
daroo Avatar asked May 02 '12 13:05

daroo


People also ask

What does the command sudo apt get install OpenCV do?

It is used for a very wide range of applications, including medical image analysis, stitching street view images, surveillance video, detecting and recognizing faces, tracking moving objects, extracting 3D models, and much more.


2 Answers

You can pass the following flag to g++:

`pkg-config --libs opencv`

For example:

g++ myprogram.cpp `pkg-config --libs opencv` -o myprogram

pkg-config give to the compiler the information about the library for you.

You can look at:

/usr/local/lib/pkgconfig/opencv.pc

This file contains all the relevant information for compilation:

prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 2.3.1
Libs: -L${libdir} -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
Cflags: -I${includedir_old} -I${includedir_new}
like image 117
user1370912 Avatar answered Oct 11 '22 21:10

user1370912


It seems to me that you are linking with the older C libraries. Today's OpenCV link commands would be more like: -lopencv_core -lopencv_flann -lopencv_highgui....

I usually link with the core module first, since order does matter, then follow the alphabet order (to be sure that I don't forget a module).

-- EDIT --

Did you try to put the -lab after opencv libs ? This may do the trick, since the libraries are linked in the order of the command line.

like image 29
sansuiso Avatar answered Oct 11 '22 21:10

sansuiso