0
$\begingroup$

Rosanswers logo

How could I compile opencv programs outside ROS?

I installed the opencv-2.3.1 package through ROS, but there seems to be link errors when compiling opencv programs outside ROS.

I created a Makefile file with the following content

DisplayImage:DisplayImage.cpp
    @g++ -L/usr/lib/ `pkg-config opencv-2.3.1 --cflags --libs` -o DisplayImage DisplayImage.cpp

But the compiling result is

XXX@ubuntu:~/Documents/programs/OpenCV$ make
/usr/bin/ld: cannot find -l/usr/lib/libopencv_contrib.so.2.3.1
/usr/bin/ld: cannot find -l/usr/lib/libopencv_legacy.so.2.3.1
/usr/bin/ld: cannot find -l/usr/lib/libopencv_objdetect.so.2.3.1
/usr/bin/ld: cannot find -l/usr/lib/libopencv_calib3d.so.2.3.1
/usr/bin/ld: cannot find -l/usr/lib/libopencv_features2d.so.2.3.1
/usr/bin/ld: cannot find -l/usr/lib/libopencv_video.so.2.3.1
/usr/bin/ld: cannot find -l/usr/lib/libopencv_highgui.so.2.3.1
/usr/bin/ld: cannot find -l/usr/lib/libopencv_ml.so.2.3.1
/usr/bin/ld: cannot find -l/usr/lib/libopencv_imgproc.so.2.3.1
/usr/bin/ld: cannot find -l/usr/lib/libopencv_flann.so.2.3.1
/usr/bin/ld: cannot find -l/usr/lib/libopencv_core.so.2.3.1
collect2: ld returned 1 exit status
make: *** [DisplayImage] Error 1

pkg-config --libs --cflags opencv-2.3.1

shows

-I/usr/include/opencv-2.3.1/opencv -I/usr/include/opencv-2.3.1 -l/usr/lib/libopencv_contrib.so.2.3.1 -l/usr/lib/libopencv_legacy.so.2.3.1 -l/usr/lib/libopencv_objdetect.so.2.3.1 -l/usr/lib/libopencv_calib3d.so.2.3.1 -l/usr/lib/libopencv_features2d.so.2.3.1 -l/usr/lib/libopencv_video.so.2.3.1 -l/usr/lib/libopencv_highgui.so.2.3.1 -l/usr/lib/libopencv_ml.so.2.3.1 -l/usr/lib/libopencv_imgproc.so.2.3.1 -l/usr/lib/libopencv_flann.so.2.3.1 -l/usr/lib/libopencv_core.so.2.3.1

How could I fix the problem? Thank you.


Originally posted by MichaelHsu170 on ROS Answers with karma: 67 on 2011-11-11

Post score: 2

$\endgroup$

3 Answers 3

0
$\begingroup$

Rosanswers logo

As far as I know, gcc requires linker option as -L/usr/lib -lopencv_core or /usr/lib/libpoencv_core.so.2.3.1 and ~-l/usr/lib/libopencv_core.so.2.3.1` is not supported. At this moment, we need following script to modify output of pkg-config to gcc compliant.

$ pkg-config --libs opencv-2.3.1 | sed 's#-l\Slib([^./]).so.2.3.1#-l\1#g' -lopencv_contrib -lopencv_legacy -lopencv_objdetect -l3d -lopencv_features2d -lopencv_video -lopencv_highgui -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core

$ pkg-config --libs opencv-2.3.1 | sed 's#-l##g' /usr/lib/libopencv_contrib.so.2.3.1 /usr/lib/libopencv_legacy.so.2.3.1 /usr/lib/libopencv_objdetect.so.2.3.1 /usr/lib/libopencv_calib3d.so.2.3.1 /usr/lib/libopencv_features2d.so.2.3.1 /usr/lib/libopencv_video.so.2.3.1 /usr/lib/libopencv_highgui.so.2.3.1 /usr/lib/libopencv_ml.so.2.3.1 /usr/lib/libopencv_imgproc.so.2.3.1 /usr/lib/libopencv_flann.so.2.3.1 /usr/lib/libopencv_core.so.2.3.1


Originally posted by Kei Okada with karma: 1186 on 2011-11-14

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by MichaelHsu170 on 2011-11-14:
Thank you very much!

$\endgroup$
0
$\begingroup$

Rosanswers logo

Hi Michael,

Generally the below issues are observed in compiling opencv2.3. As you mentioned these are linking errors. a)

 error while loading shared libraries: libopencv_core.so.2.3: cannot open shared object file:

either your file libopencv_core.so.2.3 is present in /usr/local/lib , and the default opencv directory is /usr/lib so you should change the path of your library. can be done using

export LD_LIBRARY_PATH=/usr/local/lib.

b) or there could be linking problem , which can be resolved by using

ln -s /usr/lib/libopencv_core.so.2.3 /usr/lib/libopencv_core.so.2.2

In your case i observe many such files are missing their links as in the case b. So you may have to link all of them. Before you link make sure what is available in the path and what needs to be added. Check where these files are available in the system. Do the linking after having known this.

And in your compiling you can just use

g++ -o my_example my_example.cpp `pkg-config opencv --cflags --libs`

No need of opencv-2.3.1, unless you have other conflicting version installed.

Hope this helps.

Karthik


Originally posted by karthik with karma: 2831 on 2011-11-11

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by MichaelHsu170 on 2011-11-13:
Thank you very much!

$\endgroup$
0
$\begingroup$

Rosanswers logo

Kei is correct. I just ran into this problem too, but instead of using sed, I just created an additional "/usr/lib/pkgconfig/opencv.pc" with the following diff:

12c12
< Libs:  -l${exec_prefix}/lib/libopencv_contrib.so.2.3.1 -l${exec_prefix}/lib/libopencv_legacy.so.2.3.1 -l${exec_prefix}/lib/libopencv_objdetect.so.2.3.1 -l${exec_prefix}/lib/libopencv_calib3d.so.2.3.1 -l${exec_prefix}/lib/libopencv_features2d.so.2.3.1 -l${exec_prefix}/lib/libopencv_video.so.2.3.1 -l${exec_prefix}/lib/libopencv_highgui.so.2.3.1 -l${exec_prefix}/lib/libopencv_ml.so.2.3.1 -l${exec_prefix}/lib/libopencv_imgproc.so.2.3.1 -l${exec_prefix}/lib/libopencv_flann.so.2.3.1 -l${exec_prefix}/lib/libopencv_core.so.2.3.1
---
> Libs: -L${libdir} -lopencv_contrib -lopencv_legacy -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_video -lopencv_highgui -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core

or if you just want the contents of the file:

# Package Information for pkg-config
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv-2.3.1/opencv
includedir_new=${prefix}/include/opencv-2.3.1

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 2.3.1
Libs: -L${libdir} -lopencv_contrib -lopencv_legacy -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_video -lopencv_highgui -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
Cflags: -I${includedir_old} -I${includedir_new}

Originally posted by jbohren with karma: 5809 on 2011-11-14

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by jbohren on 2011-11-17:
Thanks, for filing that, Kei!

Comment by MichaelHsu170 on 2011-11-15:
I cannot agree any more~

Comment by Kei Okada on 2011-11-15:
Yes, it would be much better if deb package maintainer update .pc files, I created ticket here -> https://code.ros.org/trac/opencv/ticket/1475

Comment by MichaelHsu170 on 2011-11-14:
Thank you very much!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.