Can C++11 be used with Arduino



As of July 2014, you can!

The Arduino development team has recently upgraded their IDE and tool chain to GCC version 4.8.1. This capability has only been built into the beta version 1.5.7, all earlier versions including the 1.0.x series do not support C++11.

Enabling C++11

Until the next version of the Arduino IDE is released (1.5.8). You will have to manually enable C++11 in the configuration files. This is only a temporary measure as the request for C++11 has already been flagged for addition into the Arduino defaults however this is yet to be merged into the release. To manually apply the modification, you need to locate the file platform.txt.

Open the location of your arduino installation, then using the table below, follow the location to the core you wish to modify.

Windows
Paths to platform.txt inside Arduino install
Core
Path
AVR {install path}\hardware\arduino\avr\platform.txt
SAM {install path}\hardware\arduino\sam\platform.txt

 

OS X

For those of you using OS X, the path to the file platform.txt file is located here:

Contents\Java\hardware\arduino\avr\platform.txt

This bit of information was kindly provided by Jessy_C, an Arduino forum member.

Applying the fix

As I installed Arduino into my D:\ drive, the path to my AVR platform.txt is D:\arduino-1.5.7\hardware\arduino\avr\platform.txt.

Inside plaform.txt you will find a list of key/value pairs. If your PC runs Windows, you will need to open the file using Wordpad, rather than Notepad; As the new line characters used do not render properly in Notepad. 

The key we are looking for is compiler.cpp.flags which is located on line 24 in the AVR version and line 20 in the SAM/Due version. Once located, we can simply append the flag to the line, just leave a single space between the last flag and this addition. For standard C++11 you will need to add -std=c++11, and for C++11 with GCC extensions active, we can use -std=gnu++11. Take note the Arduino team will be implementing the gnu version.

 For a reference, the changes are summarized below.

Changes to platform.txt to enable C++11
Core
Line
Command
AVR 24 compiler.cpp.flags=-c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -MMD -std=gnu++11
SAM 20 compiler.cpp.flags=-c -g -Os -w -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -Dprintf=iprintf -std=gnu++11

 

Any changes you make to platform.txt will not take effect until the IDE is restarted. Now you have access to the great new features of C++11 like ranged loops, constexpr, and lambdas. For a look at all the features supported by GCC, view this external link: https://gcc.gnu.org/gcc-4.8/cxx0x_status.html

Tags: Arduino, C++11, IDE
Last update:
2014-07-22 13:08
Author:
Christopher Andrews
Revision:
1.1
Average rating: 5 (4 Votes)

You can comment this FAQ

Chuck Norris has counted to infinity. Twice.