Search code examples
cmakeroscatkin

ROS top level CMakelists.txt is not used, how to give cmake defaults for all sub projects?


I have a workspace set out as in the ROS REP 128 aka:

catkin_ws
   src
      CMakeLists.txt // < 1.
      project_1
          CMakeLists.txt
      project_2
          CMakeLists.txt
      ...

In fact you can reproduce this using just the catkin tutorials here. The top level CMakeLists.txt (1. in the example above) appears to not be sourced. I can tell this because i added the line, weifnbwefn to the end of it expecting a cmake error (on a clean build) but got none.

To reproduce this:

  1. Clone the above repo.
  2. Navigate to the catkin_tutorials/create_package_pubsub/catkin_ws
  3. Modify the src/CMakeLists.txt with something that won't build.
  4. Build with catkin build

So this leads to 2 questions:

  1. Why is this file not used?
  2. How can i add default cmake items for all of my sub projects? for eg. if i wanted to add set(CMAKE_CXX_STANDARD 17) for every sub-project?

For 2. , i know that i can add additional cmake args to my build profile in .catkin_tools but for anything more complex then a couple of arguments, this would not be scaleable.


Solution

  • My understanding from the docs is that the src/CMakeLists.txt is intended to be a symlink to catkin/cmake/toplevel.cmake in the ROS installation and not be edited. It is only used if CMake is driving the legacy build workflow.

    The new workflow, which catkin build implements, ignores this file specifically to isolate the builds and prevent cross-talk from one package setting CMake cache variables another reads.

    So that answers part (1), but regrettably I am not experienced enough with catkin to know what the best practice is to store common configuration. I do know that there is a --cmake-args flag to catkin build that you could use in tandem with the CMAKE_PROJECT_INCLUDE variable to inject a CMake file containing your settings.

    For C++17 specifically, this would look like:

    $ catkin build --cmake-args -DCMAKE_CXX_STANDARD=17
    

    See the docs for this here: https://catkin-tools.readthedocs.io/en/latest/verbs/catkin_build.html#temporarily-changing-build-flags