58

How can I use C++11 when programming the Arduino?

I would be fine using either the Arduino IDE or another environment. I am most interested in the core language improvements, not things that require standard library changes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
walrii
  • 3,472
  • 2
  • 28
  • 47

6 Answers6

53

As of version 1.6.6, the Arduino IDE enables C++11 by default.

For older versions, read on:

It is very easy to change the flags for any element of the toolchain, including the assembler, compiler, linker or archiver.

Tested on the Arduino IDE version 1.5.7 (released on July 2014),

  1. Locate the platform.txt file,
  • AVR architecture => {install path}\hardware\arduino\avr\platform.txt
  • SAM architecture => {install path}\hardware\arduino\sam\platform.txt
  1. Inside that file, you can change any flag, for instance,
  • compiler.c.flags for changing the default compilation flags for C++ files.
  • compiler.cpp.flags for changing the default compilation flags for C++ files.
  1. You can also change any of the "recipes" or compile patters, at the corresponding section of the configuration file, under the title "AVR/SAM compile patterns".
  2. After making the changes, you must restart the Arduino IDE, at least on version 1.5.7.

For instance,

To enable support for C++11 (C++0x), tested on Arduino IDE versions 1.5.7 and 1.5.8, you will simply add the flag "-std=gnu++11" at the end of the line starting with compiler.cpp.flags=".

It is expected that C++11 is enabled by default in the near future on the Arduino IDE. However, as of version 1.5.8 (Oct 2014) it is still not the case.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jose.angel.jimenez
  • 2,127
  • 23
  • 17
  • 3
    You can use platform.local.txt to append flags. echo "compiler.cpp.extra_flags=-std=gnu++11" > ./hardware/arduino/avr/platform.local.txt – ticapix Dec 28 '14 at 13:13
  • 3
    At least latest (atow) Arduino IDE does have `-std=gnu++11` flag within default platform.txt. And here you could see commit [Enable C++11 support: github.com/arduino/Arduino/commit/ddf4d878fdb39173b872f69149fb4cf1cd9fd017](https://github.com/arduino/Arduino/commit/ddf4d878fdb39173b872f69149fb4cf1cd9fd017#diff-0d45b5349ab1c852b7cad3e6b173e880). Also mentioned here http://stackoverflow.com/a/33718020/1338846 – Sampo Sarrala - codidact.org Feb 23 '16 at 23:40
  • @ticapix Just to be sure, I don't think platform.local.txt works anymore (some reports here: https://github.com/arduino/Arduino/issues/3371). Btw, for Windows users, platform.txt is located at: `C:\Program Files (x86)\Arduino\hardware\arduino\avr` – Sujay Phadke Mar 26 '17 at 07:40
  • 1
    No longer true as the latest version of arduino ide is at 1.8.2 now and it has c++11 by default. – Mukunda Modell May 03 '17 at 08:31
  • 2
    What about C++17? – user643011 Aug 30 '17 at 23:24
  • For C++17 you will have to change the compiler, then it works. For recent Arduino IDE versions, the AVR compiler is still inside the Arduino tree, but the Cortex compiler is somewhere in user/settings. – Wouter van Ooijen Feb 25 '18 at 22:46
  • avr-gcc now supports C++20 – glibg10b Nov 06 '21 at 07:33
16

Arduino IDE 1.6.6 and newer have C++11 enabled by default (they have the compiler flag "-std=gnu++11" set in the platform.txt file).

Nate
  • 2,449
  • 3
  • 21
  • 29
Capt
  • 160
  • 1
  • 5
14

Firstly, only GCC 4.7 and above (and therefore AVR-GCC 4.7 and above) support C++11. So, check the versions installed with:

gcc --version
avr-gcc --version

If AVR-GCC is 4.7 or higher, then you may be able to use C++11.

The Arduino IDE does not support custom compiler flags. This has been requested, but has not yet been implemented.

So, you are left with having to use other environments or to compile your program directly from the command line.

In case, of compiling directly from the command line using AVR-GCC, you simply need to add an extra compiler flag for enabling C++11 support.

-std=c++11

For specific development environments, most would support editing of the compiler flags from the build options within the IDE. The above mentioned flag needs to be added to the list of flags for each environment.


C++0x was the name of working draft of the C++11 standard. C++0x support is available GCC 4.3 onwards. However, this is strictly experimental support so you cannot reliably expect C++11 features to be present. Here is the complete list of features available with the corresponding version of GCC. The availability of features in AVR-GCC will be the same as what's available in the corresponding GCC version.

The compiler flag for C++0x is:

-std=c++0x
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
asheeshr
  • 4,088
  • 6
  • 31
  • 50
  • Could you use C++0x? I know it's a bit more of a mouthful, but it _was_ the name of the standard until recently, and it looks much nicer IMO. – Polar Apr 12 '13 at 18:34
  • 1
    This used to be the best answer (thanks). But with new IDE releases, a newer answer on is better. – walrii Oct 03 '14 at 21:55
1

Please, note, that there is no easy way to specify additional flags from Arduino IDE or use other IDE (Eclipse, Code::Blocks, etc.) or command line.

As a hack, you can use a small proxy program (should be cross-platform):

//============================================================================
// Name        : gcc-proxy.cpp
// Copyright   : Use as you want
// Description : Based on http://stackoverflow.com/questions/5846934/how-to-pass-a-vector-to-execvp
//============================================================================

#include <unistd.h>

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

int main(int argc, char *argv[]) {
    vector<string> arguments;
    vector<const char*> aptrs;

    // Additional options, one per line
    ifstream cfg((string(argv[0]) + ".ini").c_str());
    if (cfg.bad())
        cerr << "Could not open ini file (you're using proxy for some reason, er?)" << endl;

    string arg;
    while (cfg) {
        getline(cfg, arg);
        if(arg == "\r" || arg == "\n")
            continue;
        arguments.push_back(arg);
    }

    for (const string& arg : arguments)
        aptrs.push_back(arg.c_str());

    for (int i = 1; i < argc; ++i)
        aptrs.push_back(argv[i]);

    // Add null pointer at the end, execvp expects NULL as last element
    aptrs.push_back(nullptr);

    // Pass the vector's internal array to execvp
    const char **command = &aptrs[0];

    return execvp(command[0], command);
}
  1. Compile the program.
  2. Rename the original avr-g++.exe to avr-g++.orig.exe (or any other name).
  3. Create avr-g++.ini file where the first line is FULL path to the original program (e.g. D:\Arduino\hardware\tools\avr\bin\avr-g++.orig.exe) and add additional parameters, one per line, as desired.

You're done!

Example avr-g++.ini:

D:\Arduino\hardware\tools\avr\bin\avr-g++.orig.exe
-std=c++0x
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fAX
  • 1,451
  • 1
  • 10
  • 11
1

I use Ino and this worked:

ino build -cppflags="-std=c++0x"

This generated a hex file at least 15k in size (that's with optimizations turned on), compared to about 5k for the standard build, which is a consideration for a poor little ATmega328. It might be okay for one of the microcontrollers with a lot more program space.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Shroyer
  • 3,499
  • 2
  • 28
  • 54
-1

If you need more control and a better IDE, I recommend using Sloeber Plugin for Eclipse or the Sloeber IDE itself.

Creating more complicated code is much easier using this IDE. It also allows to add flags to the compiler (C, C++ and linker). So to customize the compile, just right click on project and select Properties. In the Properties window, select Arduino → Compiler Option. This way you can add options to your build.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Saadat
  • 461
  • 6
  • 9