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

What is the best way for CMakeLists.txt to check if ROS is installed?

asked 2016-02-25 15:52:01 -0500

liangfok gravatar image

updated 2016-02-25 15:55:27 -0500

I'm working on a toolbox written in C++ that is outside of the ROS ecosystem but want to conditionally add ROS support if ROS is installed on the local machine. What is the best way to check if ROS is installed in my toolbox's CMakeLists.txt?

I'm currently checking for the existance of a local environment variable called "ROS_ROOT" as shown below.

  find_package(roscpp REQUIRED)

Is this an acceptable solution? Is there a better solution?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-02-25 17:09:10 -0500

gerkey gravatar image

From CMake, I would use the result from find_package() without REQUIRED. For example, if you know that you need roscpp:

find_package(roscpp QUIET)
if(NOT roscpp_FOUND)
  message(STATUS "Couldn't find roscpp, so not building ROS-specific functionality. Did you source the ROS file?")
  message(STATUS "Found roscpp installed at ${roscpp_DIR}, so building ROS-specific functionality.")

You can do the same for each ROS package that you need, if they're all optional. Or you could use a common package like roscpp as a global indicator to turn on or off ROS support, and just make all the others REQUIRED.

Of course, as @jackie said, you first need to have configured your environment so that find_package() can locate ROS packages. But I think that that's better than trolling through the file system looking for ROS. Among other things, it leaves it open to the user to not configure his or her environment to be able to locate ROS packages, thereby disabling the optional ROS support.

edit flag offensive delete link more

answered 2016-02-25 16:49:57 -0500

jackie gravatar image

The ROS_ROOT environment variable will only be defined if the ROS setup.bash (e.g. /opt/ros/<distro>/setup.bash) has been sourced in the environment from which CMake was called. So for this solution you need guarantee that, if there is a ROS install, the ROS setup.bash was sourced before calling cmake.

Alternatively, if you know ROS was installed from a debian package and not from source, you can check if the folder /opt/ros/<distro> exists. However this solution is not entirely portable.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-02-25 15:52:01 -0500

Seen: 1,577 times

Last updated: Feb 25 '16