Rosanswers logo

Recently I had an issue building my own package depending on cv_bridge package and a custom-built opencv library. I was able to solve the linking problem in previous ROS versions here, but later I found out that the package was in fact using the fuerte/include/opencv header, but linking with the right /usr/local/lib libs. Everything should be OK if the headers were not changed, but if they do, it will fail.

To find out why I listed all the include dirs in cmake, and I got the following result(only the first 5 items)

-- --> include dir='/home/kai/Projects/my_pkg/include'
-- --> include dir='/opt/ros/fuerte/include'
-- --> include dir='/usr/local/include/opencv'
-- --> include dir='/usr/local/include'
-- --> include dir='/opt/ros/fuerte/stacks/vision_opencv/cv_bridge/include'

As can be seen, the ros/fuerte/include dir precedes the /usr/local/include; as a result, even if I can find my custom-built opencv headers and libraries through FindPackage() in CMakeLists.txt from setting the right $CMAKE_PREFIX_PATH env var, the #include <opencv.h> will still find the ros/fuerte/include/opencv first.

To reproduce this problem, try the following:

  1. Install ros fuerte opencv package
  2. Download opencv from opencv.org and make&make install it with your own build options(namely, with CUDA)
  3. Create a package depending on cv_bridge package
  4. Follow the link provided above to make sure your CMakeLists.txt can use FindPackage() to find your custom built opencv package(default in /usr/local)
  5. Write some pieces of code using opencv's specific features(CUDA, if you built with it)
  6. rosmake or cmake ..& make it.
  7. Check the depend.make file under your_pkg/build/CMakeFiles/your_pkg.dir, find opencv occurrences, it will be /opt/ros/fuerte/include/opencv, not /usr/local/include/opencv

So what I am writing to ask is, is there any way to make my own opencv include path precedes the ros include path? Or it won't be found because there is a opencv/opencv2 dir in the fuerte/include dir and they will be found first. Currently I renamed ROS's provided opencv&opencv2 dirs names to hide them from being found, but I think there should be some better way.

Thanks in advance.

Originally posted by K Chen on ROS Answers with karma: 391 on 2013-05-01

Post score: 7

Original comments

Comment by K Chen on 2013-05-01:
And, I am under ubuntu 12.04 32-bit. No catkin build or ros_ws(still uses the old fashioned $ROS_PACKAGE_PATH way).


2 Answers 2


Rosanswers logo

I cheated and went to /opt/ros/fuerte/include and changed the name of opencv2 to opencv2bak. This way it finds the right includes. I don't think it's the right way to do it, though.

Also, went to /opt/ros/fuerte/share/OpenCV/OpenCVConfig.cmake and edited the Install path variable to /usr/local (where I installed)

Originally posted by DiogoRolo with karma: 56 on 2013-05-17

This answer was ACCEPTED on the original site

Post score: 2

Original comments

Comment by K Chen on 2013-05-24:
Yes, this is what I am doing right now, but this would be overwritten when it updates, so I am asking for some more "elegant" way

Comment by 130s on 2014-05-12:
We should always note for future reference that modifying protected location like under /opt isn't recommended.


Rosanswers logo

I had the same problem. The thing is ros sets CPATH variable to something like:


Then when compiling gcc always finds ROS opencv headers instead of the new ones... So I ended up just deleting CPATH whenever I need to compile somthing with the new OpenCV version.

Originally posted by Jep with karma: 195 on 2014-02-17

This answer was NOT ACCEPTED on the original site

Post score: 1


Your Answer

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