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

Custom OpenCV with ROS Groovy

asked 2013-09-04 08:54:05 -0600

RossK gravatar image

updated 2013-11-14 11:12:37 -0600

tfoote gravatar image

I have a project which requires OpenCV with cuda support, so I have a local version of OpenCV compiled with cuda support, and I want to use this in ROS. In previous versions of ROS I achieved this by overlaying opencv2, which I made point to my custom OpenCV version.

Now I am trying to achieve this with CMake, with limited success.

I made a simple test package (using rosbuild), only depending on roscpp (no opencv, no cv_bridge)

The code looks like this:

#include <opencv2/core/core.hpp>
#include <iostream>

int main(int argc, char** argv)
  cv::Mat M(2,2, CV_8UC3, cv::Scalar(0,0,255));
  std::cout << "M = " << std::endl << " " << M << std::endl;
  return 0;

My CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 2.4.6)

# Set the build type.  Options are:
#  Coverage       : w/ debug symbols, w/o optimization, w/ code-coverage
#  Debug          : w/ debug symbols, w/o optimization
#  Release        : w/o debug symbols, w/ optimization
#  RelWithDebInfo : w/ debug symbols, w/ optimization
#  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
#set(ROS_BUILD_TYPE RelWithDebInfo)


#set the default path for built executables to the "bin" directory
#set the default path for built libraries to the "lib" directory

### OpenCV #############################

MESSAGE("opencv incl " ${OpenCV_INCLUDE_DIRS})
MESSAGE("opencv lib " ${OpenCV_LIBS})

#uncomment if you have defined messages
#uncomment if you have defined services

#common commands for building c++ executables and libraries
#rosbuild_add_library(${PROJECT_NAME} src/example.cpp)
#target_link_libraries(${PROJECT_NAME} another_library)
#rosbuild_link_boost(${PROJECT_NAME} thread)
rosbuild_add_executable(main src/main.cpp)
target_link_libraries(main ${OpenCV_LIBS})

I verified that ${OpenCV_INCLUDE_DIRS} and ${OpenCV_LIBS} are pointing to the correct opencv (in my case ~/svslocal/include and ~/svslocal/lib)

I then compile my code and run it, no problems. However when I run ldd on the executable, it is using the ros opencv_core instead of my local one.

kir1pal@pal3ea910:~/ros_workspace/custom_opencv_test/bin$ ldd main =>  (0x00007fff919ff000) => /opt/ros/groovy/lib/ (0x00007f90f16b9000) => /usr/lib/x86_64-linux-gnu/ (0x00007f90f1391000) => /lib/x86_64-linux-gnu/ (0x00007f90f117b000) => /lib/x86_64-linux-gnu/ (0x00007f90f0dbc000) => /lib/x86_64-linux-gnu/ (0x00007f90f0ba4000) => /lib/x86_64-linux-gnu/ (0x00007f90f0987000) => /lib/x86_64-linux-gnu/ (0x00007f90f077f000) => /lib/x86_64-linux-gnu/ (0x00007f90f0482000)
/lib64/ (0x00007f90f1af9000)

This is pretty important because if I try it on my project with CUDA I get a runtime error telling me that the opencv library has been compiled without cuda.



I also tried adding /home/kir1pal/svslocal/lib to the front of the LD_LIBRARY_PATH, did not change anything

Any idea what is going wrong here?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-09-04 12:38:20 -0600

RossK gravatar image

So the workaround I found was to add


BEFORE rosbuild_init() in my CMakeLists.txt

I found this out by first looking at my gcc line:

/usr/bin/c++   -O2 -g     -pthread CMakeFiles/main.dir/src/main.cpp.o  -o ../bin/main -rdynamic -L/home/kir1pal/svslocal/lib -L/opt/ros/groovy/lib -lroscpp -l:/usr/lib/ -l:/usr/lib/ -lrosconsole -l:/usr/lib/ -l:/usr/lib/ -lxmlrpcpp -lroscpp_serialization -lrostime -l:/usr/lib/ -l:/usr/lib/ -l:/usr/lib/ -lpthread -lcpp_common /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ /home/kir1pal/svslocal/lib/ -Wl,-rpath,/opt/ros/groovy/lib

At the end is -rpath,/opt/ros/groovy/lib, which seems to be set by cmake macro LINK_DIRECTORIES. This seemed the be the cause of the problem. /opt/ros/groovy/lib is added to LINK_DIRECTORIES by rosbuild_init() and therefore by calling LINK_DIRECTORIES before rosbuild_init() puts my library path in front of it.

This is not an ideal solution. If anyone has a better idea I would like to hear it

edit flag offensive delete link more

Question Tools



Asked: 2013-09-04 08:54:05 -0600

Seen: 840 times

Last updated: Sep 04 '13