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

ROS using C++ 11 & How to Use With Catkin?

asked 2015-08-31 23:20:33 -0500

Rud gravatar image

Is ROS compiled using c++ 11 with catkin? I suspect not since I am getting an error trying to use {} initializer for a vector, i.e. c++98...must be initialized...

For at least my builds where in the catkin files would I add the '-std=c++11' option?

I am using Eclipse but I think this is controlled via the cmake files, not a project property.

edit retag flag offensive close merge delete

Comments

I poked around and found that adding the "-std-c++11" in the file flags.make worked. But I suspect it will be overwritten the next time I add a file to the CMakeLists.txt. Just wanted to mention this with the warning. The file was overwritten with the changes to the C*.txt file.

Rud gravatar image Rud  ( 2015-09-01 23:51:45 -0500 )edit

Both answers provided worked. I flagged the one by Julius as 'correct' since it was more succinct. I added the line to just below the ## Build ## box, just fyi. Appreciated also the link to ROS/C++11 points.

Rud gravatar image Rud  ( 2015-09-01 23:57:44 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
25

answered 2015-09-01 00:55:34 -0500

JuliusGel gravatar image

updated 2017-07-11 03:03:23 -0500

All the current distributions of ROS are compiled using C++03.

You can compile your own packages using c++11 by adding the following line to the CMakeLists.txt of your project:

add_compile_options(-std=c++11)

Note that it is recomended to have checks for c++11 features at configure time and provide equivalent functionality with extra compiler features. This is only required if you are planning to release the ROS packages that you develop. If you are only planning to use your packages yourself then you can just c++11 functionality. You can read more about ROS and C++11 here: link text

edit flag offensive delete link more

Comments

Thanks for the answer. I am at the moment only developing for myself. It will be awhile before I would attempt adding to ROS itself.

Rud gravatar image Rud  ( 2015-09-01 23:47:55 -0500 )edit

Worked like a charm for me too. Thanks!

marcozorzi gravatar image marcozorzi  ( 2016-06-02 03:27:49 -0500 )edit

Is there a way to add this flag for an individual file and not the entire project?

vik748 gravatar image vik748  ( 2017-07-10 14:03:36 -0500 )edit

You could try SET_SOURCE_FILES_PROPERTIES( foo.cpp PROPERTIES COMPILE_FLAGS -std=c++11). I have, however, not tried this and I am not sure how this will affect ABI compatibility in your executable.

JuliusGel gravatar image JuliusGel  ( 2017-07-11 02:58:53 -0500 )edit

@JuliusGel; thanks man, it works for me. :)

Ashkr gravatar image Ashkr  ( 2020-07-01 04:43:17 -0500 )edit
12

answered 2015-09-01 01:33:49 -0500

You have to add

set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")

to the CMakeLists.txt of your package.

edit flag offensive delete link more

Comments

2

Also consult this: https://gcc.gnu.org/wiki/Cxx11AbiComp... Not sure if ROS is compiled to be ABI compatible between C++ versions.

dornhege gravatar image dornhege  ( 2015-09-01 06:01:20 -0500 )edit

Thanks for the answer.

Rud gravatar image Rud  ( 2015-09-01 23:48:21 -0500 )edit

Question Tools

6 followers

Stats

Asked: 2015-08-31 23:20:33 -0500

Seen: 28,047 times

Last updated: Jul 11 '17