0

Rosanswers logo

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.

if(DEFINED ENV{ROS_ROOT})
  find_package(roscpp REQUIRED)
  include_directories(${roscpp_INCLUDE_DIRS})
  ...
endif()

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


Originally posted by liangfok on ROS Answers with karma: 328 on 2016-02-25

Post score: 0

2 Answers2

1

Rosanswers logo

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 setup.sh file?")
else()
  message(STATUS "Found roscpp installed at ${roscpp_DIR}, so building ROS-specific functionality.")
  include_directories(${roscpp_INCLUDE_DIRS})
  #....
endif()

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.


Originally posted by gerkey with karma: 1981 on 2016-02-25

This answer was NOT ACCEPTED on the original site

Post score: 4

0

Rosanswers logo

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.


Originally posted by jackie with karma: 296 on 2016-02-25

This answer was NOT ACCEPTED on the original site

Post score: 2