Ask Your Question

Path to resource files in C++ node

asked 2013-08-20 03:03:24 -0600

Huibuh gravatar image

updated 2014-01-28 17:17:42 -0600

ngrennan gravatar image

Hello fellow developers,

I have trouble determining the path to a folder containing resource files in a self written C++ catkin package. I am using the following macro in CMakeLists.txt to export the resource files (a couple of .xml files btw.):

install(DIRECTORY resources

This creates the following "resources" folder:


I need to access the resource files in my source code here:

string cascade_path = "/home/ros/catkin_ws/src/opencv_ros_node/src/resources/haarcascade.xml";
haar_cascade.load( cascade_path );

Obviously, I do not want to use a hardcoded absolute path. But how can I determine the correct relative path to the resource folder during run time? (Ideally, this should also work if 'catkin_make install' has not been run explicitly)

Any help is appreciated.

Cheers, Heiko

edit retag flag offensive close merge delete


I guess you'd be OK if it's in PKG/resources as installed and not PKG/src/resources (or anywhere generically reachable). The src/ directory might not exist for installations.

dornhege gravatar image dornhege  ( 2013-08-20 03:13:11 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2013-08-20 03:11:39 -0600

dornhege gravatar image

Use the ros::package API:

ros::package::getPath('PKG_NAME') should give you the package path, where you can append your relative resource path to get a full path.

(under the assumption that this hasn't changed for catkin)

edit flag offensive delete link more



Awesome, thanks for your quick help! The only thing is that I now have to hard code 'PKG_NAME'. Is there a command to determine the name of the package this command is executed in?

Huibuh gravatar image Huibuh  ( 2013-08-20 03:53:56 -0600 )edit

I guess now it depends on what you want to do: If it's the package the source is in (or always the same), hardcoding would be OK. If that's not the case you can read it from a parameter or similar. Resources are usually given as package://MY_PKG/foo. If you need an open file: pkg: resource_retriever

dornhege gravatar image dornhege  ( 2013-08-20 04:49:20 -0600 )edit

The proposes API is still valid with catkin. It resolves the path of the package.xml file which is under INSTALL_PREFIX/share/PKGNAME. You should make sure that the relative path from there is the same as in the source checkout of your package. That ensures that it is found in both devel- and install space. For your example with the resources folder that is the case.

Dirk Thomas gravatar image Dirk Thomas  ( 2013-08-20 06:41:39 -0600 )edit

Thanks guys, I got it working :-)

Huibuh gravatar image Huibuh  ( 2013-08-20 20:52:03 -0600 )edit

Is there a way to retrieve the name of the package in which a node is contained? eg: ros::package::getPath(ros::this_package::getName())?

VictorLamoine gravatar image VictorLamoine  ( 2017-02-07 09:18:06 -0600 )edit

Thanks for the help!

antoineniotna gravatar image antoineniotna  ( 2019-05-07 03:35:40 -0600 )edit

I keep getting ‘ros::package’ has not been declared errors all the way.

Jägermeister gravatar image Jägermeister  ( 2019-05-28 04:31:11 -0600 )edit

#include <ros/package.h>

VictorLamoine gravatar image VictorLamoine  ( 2019-05-28 14:18:13 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2013-08-20 03:03:24 -0600

Seen: 10,636 times

Last updated: Aug 20 '13