ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

about compiling opencv programs outside ROS

asked 2011-11-11 14:24:39 -0500

MichaelHsu170 gravatar image

updated 2012-02-27 13:30:41 -0500

kwc gravatar image

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.

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted
3

answered 2011-11-14 03:27:01 -0500

Kei Okada gravatar image

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

edit flag offensive delete link more

Comments

Thank you very much!
MichaelHsu170 gravatar image MichaelHsu170  ( 2011-11-14 22:43:42 -0500 )edit
1

answered 2011-11-14 04:28:57 -0500

jbohren gravatar image

updated 2011-11-14 04:39:30 -0500

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}
edit flag offensive delete link more

Comments

Thank you very much!
MichaelHsu170 gravatar image MichaelHsu170  ( 2011-11-14 22:43:59 -0500 )edit
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
Kei Okada gravatar image Kei Okada  ( 2011-11-15 13:57:34 -0500 )edit
I cannot agree any more~
MichaelHsu170 gravatar image MichaelHsu170  ( 2011-11-15 15:07:34 -0500 )edit
Thanks, for filing that, Kei!
jbohren gravatar image jbohren  ( 2011-11-17 15:32:32 -0500 )edit
0

answered 2011-11-13 08:37:06 -0500

MichaelHsu170 gravatar image

Thank you very much! But I'm confronted with an installation problem. From the night before yesterday, it always shows "Unmet dependencies in ros-electric-desktop-full installation", so I cannot install ROS right now... Before that night installations of ros-electric-desktop-full succeeded every time. Is there any idea about how to fix the problem? Thank you.

edit flag offensive delete link more
1

answered 2011-11-11 21:33:20 -0500

karthik gravatar image

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

edit flag offensive delete link more

Comments

Thank you very much!
MichaelHsu170 gravatar image MichaelHsu170  ( 2011-11-13 08:32:10 -0500 )edit

Question Tools

Stats

Asked: 2011-11-11 14:24:39 -0500

Seen: 3,207 times

Last updated: Nov 14 '11