The primary thing that happens when you source your catkin workspace's devel/setup.bash
file is that you are setting environment variables.
For example, you'll notice a difference in many environment variables before & after sourcing the setup file:
- ROS_PACKAGE_PATH: This tells ROS where to find packages, so that
rosrun <pkg> <node>
actually works.
- CMAKE_PREFIX_PATH: This tells the compiler a bunch of stuff, including where to find the libraries defined in your package.
- LD_LIBRARY_PATH: This is used at execution time to dynamically load shared libraries.
- PYTHONPATH: This tells python where to look for any python modules defined in your package.
(There are others, there are just the important ones).
As an example, this is my ROS_PACKAGE_PATH before and after sourcing the setup file in some random catkin workspace:
$ cd /path/to/my/catkin_ws
$ echo $ROS_PACKAGE_PATH
/opt/ros/indigo/share:/opt/ros/indigo/stacks
$ source devel/setup.bash
$ echo $ROS_PACKAGE_PATH
/path/to/my/catkin_ws/src:/opt/ros/indigo/share:/opt/ros/indigo/stacks
Note the addition of my catkin workspace's src/
directory to the ROS_PACKAGE_PATH. This enables me to run any nodes I have defined in my workspace, otherwise only the system-installed packages are available. (Side note: /opt/ros/<distro>/
basically acts as one big catkin workspace too.)
Normally, you always source the global /opt/ros/<distro>/setup.bash
every time you open a terminal because it's in your .bashrc
file. However, since new terminal sessions don't carry over environment variables, you need to source your workspace's setup file again each time you open a new session.
If you do not want to have to source the workspace setup file yourself each time you open a new terminal, you can always add it to your .bashrc
file:
echo "source /path/to/your/workspace/devel/setup.bash" >> ~/.bashrc
Obviously this only works if you have a single catkin workspace.
Originally posted by Felix Duvallet with karma: 539 on 2016-08-11
This answer was ACCEPTED on the original site
Post score: 7
Original comments
Comment by jarvisschultz on 2016-08-11:
Very helpful answer! Technically the LD_LIBRARY_PATH
is not for any compiler, it's for used by ld.so
at execution time to dynamically load shared libraries. Without it, you could have a piece of code that compiles but doesn't run.
Comment by jarvisschultz on 2016-08-11:
That situation would depend on things like ldconfig and /etc/ld.so.cache
, and the -rpath
flag of gcc/g++ (see this tutorial, step 4). The CMAKE_PREFIX_PATH is like what you described.
Comment by jarvisschultz on 2016-08-11:
It is important for catkin/CMake to be able to find ROS libraries, and it is controlled by the setup.bash scripts.
Comment by Felix Duvallet on 2016-08-11:
Thanks for the comment, you're absolutely right. I've updated the answer. I think the link to the shared libraries tutorial is still helpful as a comment.
Comment by alienmon on 2016-08-14:
So everytime a new terminal is opened, it automatically source what's in bashrc file. And /opt/ros//setup.bash is in bashrc file, that's why I don't have to source it everytime i open terminal. While source devel/setup.bash is not, and that's why I have to source evertytime i open new term.