I am trying to compile and test our packages from tu-darmstadt-ros-pkg with the upcoming ROS Fuerte from the official package repositories with Ubuntu Oneiric (amd64). Whenever a package is built which depends indirectly from OpenCV, I got strange linker errors about undefined references for OpenCV functions.
In this case I am trying to compile the object_tracker package, which depends from image_geometry, which uses OpenCV. This is the output from make VERBOSE=1:
Linking CXX executable ../../bin/object_tracker
cd /opt/hector/external/tu-darmstadt-ros-pkg/hector_worldmodel/object_tracker/build/src && /usr/bin/cmake -E cmake_link_script CMakeFiles/object_tracker.dir/link.txt --verbose=1
/usr/lib/ccache/c++ -m64 -O2 -g -Wl,-rpath,/opt/ros/fuerte/stacks/geometry/tf/lib -I/opt/ros/fuerte/stacks/geometry/tf/msg_gen/cpp/include -I/opt/ros/fuerte/stacks/geometry/tf/srv_gen/cpp/include -Wl,-rpath,/opt/ros/fuerte/stacks/bullet/lib -Wl,-rpath,/opt/ros/fuerte/stacks/geometry/tf_conversions/lib -Wl,-rpath,/opt/ros/fuerte/stacks/orocos_kinematics_dynamics/orocos_kdl/install_dir/lib /opt/ros/fuerte/lib/libopencv_calib3d.so /opt/ros/fuerte/lib/libopencv_contrib.so /opt/ros/fuerte/lib/libopencv_core.so /opt/ros/fuerte/lib/libopencv_features2d.so /opt/ros/fuerte/lib/libopencv_flann.so /opt/ros/fuerte/lib/libopencv_gpu.so /opt/ros/fuerte/lib/libopencv_highgui.so /opt/ros/fuerte/lib/libopencv_imgproc.so /opt/ros/fuerte/lib/libopencv_legacy.so /opt/ros/fuerte/lib/libopencv_ml.so /opt/ros/fuerte/lib/libopencv_objdetect.so /opt/ros/fuerte/lib/libopencv_stitching.so /opt/ros/fuerte/lib/libopencv_ts.so /opt/ros/fuerte/lib/libopencv_video.so -Wl,-rpath,/opt/ros/fuerte/stacks/vision_opencv/image_geometry/lib /opt/ros/fuerte/lib/libopencv_calib3d.so /opt/ros/fuerte/lib/libopencv_contrib.so /opt/ros/fuerte/lib/libopencv_core.so /opt/ros/fuerte/lib/libopencv_features2d.so /opt/ros/fuerte/lib/libopencv_flann.so /opt/ros/fuerte/lib/libopencv_gpu.so /opt/ros/fuerte/lib/libopencv_highgui.so /opt/ros/fuerte/lib/libopencv_imgproc.so /opt/ros/fuerte/lib/libopencv_legacy.so /opt/ros/fuerte/lib/libopencv_ml.so /opt/ros/fuerte/lib/libopencv_objdetect.so /opt/ros/fuerte/lib/libopencv_stitching.so /opt/ros/fuerte/lib/libopencv_ts.so /opt/ros/fuerte/lib/libopencv_video.so -I/opt/hector/external/tu-darmstadt-ros-pkg/hector_worldmodel/worldmodel_msgs/msg_gen/cpp/include -I/opt/hector/external/tu-darmstadt-ros-pkg/hector_worldmodel/worldmodel_msgs/srv_gen/cpp/include -I/opt/hector/external/tu-darmstadt-ros-pkg/hector_slam/hector_nav_msgs/srv_gen/cpp/include -pthread CMakeFiles/object_tracker.dir/object_tracker.o CMakeFiles/object_tracker.dir/Object.o CMakeFiles/object_tracker.dir/ObjectModel.o -o ../../bin/object_tracker -rdynamic -L/opt/ros/fuerte/lib -L/opt/ros/fuerte/stacks/geometry/tf/lib -L/opt/ros/fuerte/stacks/bullet/lib -L/opt/ros/fuerte/stacks/geometry/tf_conversions/lib -L/opt/ros/fuerte/stacks/orocos_kinematics_dynamics/orocos_kdl/install_dir/lib -L/opt/ros/fuerte/stacks/vision_opencv/image_geometry/lib -ltf -lboost_thread-mt -lBulletDynamics -lBulletCollision -lLinearMath -lsensor_msgs -lmessage_filters -lroscpp -lrostime -lrosconsole -lroscpp_serialization -lxmlrpcpp -ltf_conversions -lorocos-kdl -limage_geometry -Wl,-rpath,/opt/ros/fuerte/lib:/opt/ros/fuerte/stacks/geometry/tf/lib:/opt/ros/fuerte/stacks/bullet/lib:/opt/ros/fuerte/stacks/geometry/tf_conversions/lib:/opt/ros/fuerte/stacks/orocos_kinematics_dynamics/orocos_kdl/install_dir/lib:/opt/ros/fuerte/stacks/vision_opencv/image_geometry/lib
/usr/bin/ld: CMakeFiles/object_tracker.dir/object_tracker.o: undefined reference to symbol 'cv::fastFree(void*)'
/usr/bin/ld: note: 'cv::fastFree(void*)' is defined in DSO /opt/ros/fuerte/lib/libopencv_core.so so try adding it to the linker command line
/opt/ros/fuerte/lib/libopencv_core.so: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
distcc[28708] ERROR: compile (null) on localhost failed
make[3]: *** [../bin/object_tracker] Error 1
make[3]: Leaving directory `/opt/hector/external/tu-darmstadt-ros-pkg/hector_worldmodel/object_tracker/build'
make[2]: *** [src/CMakeFiles/object_tracker.dir/all] Error 2
make[2]: Leaving directory `/opt/hector/external/tu-darmstadt-ros-pkg/hector_worldmodel/object_tracker/build'
make[1]: *** [all] Error 2
make[1]: Leaving directory `/opt/hector/external/tu-darmstadt-ros-pkg/hector_worldmodel/object_tracker/build'
make: *** [all] Error 2
As you can see, the OpenCV libraries are included in the linker command line, and ld seems to know that the unresolved symbol cv::fastFree is defined in libopencv_core.so. Both, the opencv2 and the image_geometry package are exporting the OpenCV compiler and linker flags in their manifests. I have no idea why the symbol cannot be resolved and what the "could not read symbols" error message is trying to tell me.
I know that I have to include the find_package(OpenCV REQUIRED) line in my CMakeLists.txt and link my target to the ${OpenCV_LIBRARIES} as explained on the opencv2 wiki page. Applying this to the object_tracker project solves the problem, but I am not using OpenCV directly. Do I have to explicitly link to the OpenCV libraries in packages depending from OpenCV indirectly?
You can look at the object_tracker sources here: http://code.google.com/p/tu-darmstadt-ros-pkg/source/browse/?r=364#svn/trunk/hector_worldmodel/object_tracker
With some other packages I have the same problem, only the names of the unresolved symbols differ.
Originally posted by Johannes Meyer on ROS Answers with karma: 1266 on 2012-04-19
Post score: 2