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

What happens if I use c++ 17 features in my ros nodes?

asked 2020-01-09 05:31:12 -0600

max11gen gravatar image

In the ROS manual/wiki you can see, that ROS is only made for C++11. But what exactly does that mean? What happens if I just put add_compile_options(-std=c++17) in the CMakeLists.txt of my ROS-package and use C++17-features anyway? Specifically: Is it possible to use parallel std::for_each from C++17 in my ROS-nodes?

edit retag flag offensive close merge delete

Comments

1

In the ROS manual/wiki you can see

please always link to pages you are referring to. Right now we don't know what you "see" exactly.

gvdhoorn gravatar image gvdhoorn  ( 2020-01-09 06:36:08 -0600 )edit
1

@gvdhoorn I know, sorry. The thing was just, that I couldn't actually find where exactly I had read that, but I rather just had it in the back of my head.

max11gen gravatar image max11gen  ( 2020-01-09 08:13:52 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
6

answered 2020-01-09 06:35:18 -0600

gvdhoorn gravatar image

ROS is only made for C++11

this is actually not really true.

For ROS 1: Melodic has lifted the max version to C++14 (see here).

For ROS 2: all ROS 2 versions target C++14 by default (see REP-2000, search for "Minimum language requirements").

What happens if I just put add_compile_options(-std=c++17) in the CMakeLists.txt of my ROS-package and use C++17-features anyway?

Nothing. It probably will just work, as long as your C++17 object code is ABI compatible with whatever libraries you are linking against.

In other words: you'll potentially run into the exact same problems you could have with ABI incompatibilities between libraries when not using ROS.

edit flag offensive delete link more

Comments

Thanks for your answer. But how can I find out, if the object code is ABI compatible after all? Will I get errors if the compatibility is not given, or can it happen that there will be just arbitrary, undefined behaviour occurring?

max11gen gravatar image max11gen  ( 2020-01-09 08:12:36 -0600 )edit

Yes, could be linking errors, could also be SEGFAULTs. I can't give you a more definitive answer unfortunately.

As I wrote: there is nothing really ROS specific here. It's essentially plain C++.

From my own personal experience though: C++03 + C++11 was troublesome (std::string etc). C++11 and newer has not been a problem for me so far (ie: combining binary artefacts compiled with these different versions). But again: personal experience, so this is not a guarantee everything will work.

gvdhoorn gravatar image gvdhoorn  ( 2020-01-09 08:16:10 -0600 )edit
1

In addition to above-mentioned, you can also use the set_properties macro in CMakeLists to set cpp standards for specific targets only (where you know you need c++17 for example) within a package instead for the whole package as it is done with add_compile_options. Here is an example which applies the c++17 standard for a defined executable:

set_property(TARGET my_executable PROPERTY CXX_STANDARD 17)
set_property(TARGET my_executable PROPERTY CXX_STANDARD_REQUIRED ON)
pavel92 gravatar image pavel92  ( 2020-01-09 08:58:10 -0600 )edit

@gvdhoorn Alright, thanks for your help!

max11gen gravatar image max11gen  ( 2020-01-09 09:51:35 -0600 )edit

@pavel92 Great hint, thanks.

max11gen gravatar image max11gen  ( 2020-01-09 09:51:57 -0600 )edit

I would use target_compile_features(..) with a meta-feature instead, but it depends on your CMake version whether that is available.

gvdhoorn gravatar image gvdhoorn  ( 2020-01-09 10:14:57 -0600 )edit
1

We do not have any issues with linking our C++17 code to noetic. Everything builds and runs just fine, with all tests passing.

audrius gravatar image audrius  ( 2021-05-23 04:29:28 -0600 )edit

It depends a bit on the compilers in use. For example, if you use one version of GCC to build everything then you are fine to link C++11, 14, and 17 code together (see https://stackoverflow.com/a/49119902/...)

allsey87 gravatar image allsey87  ( 2022-05-25 05:14:56 -0600 )edit
2

answered 2020-06-09 03:02:48 -0600

vilas gravatar image

Well it certainly breaks with C++20, both ROS Melodic and Noetic. Just beware.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2020-01-09 05:31:12 -0600

Seen: 7,697 times

Last updated: Jan 09 '20