How do you "colcon build" with a specific C++ version?
Problem (TL;DR)
How do I run colcon build
with the C++ version set specifically to 17, and without having to manually edit every CMakeList file in my system?
Problem (in detail)
I am currently encountering a problem whilst compiling on a Ubuntu 20 system, with ROS Galactic and ROS2 Foxy both installed. I want to compile my ROS2 code using Galactic (not Foxy) so I have made sure that I have sourced only the setup file for that distro. But when I run colcon build
I get a nebulous error that suggests the problem is that a pre-C++17 standard is being used:
/opt/ros/galactic/include/rclcpp/any_subscription_callback.hpp:81:29: error: ‘variant’ in namespace ‘std’ does not name a
template type
81 | using variant_type = std::variant<
| ^~~~~~~
/opt/ros/galactic/include/rclcpp/any_subscription_callback.hpp:81:24: note: ‘std::variant’ is only available from C++17 onwards
81 | using variant_type = std::variant<
| ^~~
This error doesn't reference any of my code directly, and seems to indicate that the rclcpp package in Galactic is failing to compile because C++17 isn't being used. I'm surprised by this as there are a a few posts around the place suggesting that Galactic requires C++17 (see here) but I couldn't find any reference to this on the Galactic release page. This also doesn't appear to be a known issue with rclcpp (see the issue tracker)
I couldn't find any reference to "CXX" or "C++" versions in the colcon documentation either, but through colcon build help
figured out that the following command should in _theory_ set the C++ version:
$ colcon build --cmake-args -DCMAKE_CXX_FLAGS='-std=c++17'
However this doesn't appear to work as I get the same error. I know that I _can_ force individual packages to compile with a specific version using this code snippet in the CMakeList file:
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
However, I would rather not have to change the CMakeList files of all the packages in my codebase, and I'm pretty sure that changing the version via colcon build
should have worked. So my questions are:
- How do you force colcon to use a specific C++ standard?
- Why didn't setting it via
-DCMAKE_CXX_FLAGS='-std=c++17'
work? - If this isn't the solution to the std::variant build error, can someone please point me in the direction of the solution?
I suspect that the cause of the std::variant build error has something to do with the fact that I have Foxy installed on the same system. But I haven't worked out why yet.
how did you install ROS?
Docker container with an image pulled from osrf/ros:foxy-desktop or osrf/ros:galactic-desktop.