How to use C ++ 11 to program Arduino?

How can I use c++11 when programming Arduino? I would be fine using either the Arduino IDE or another environment. What interests me most is improvements to the main language, not things that require standard changes to the library.

+43
c ++ ide arduino firmware
Apr 12 '13 at 16:27
source share
5 answers

As in version 1.6.6 , the Arduino IDE allows you to use C ++ 11 by default.

For older versions read:

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

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

  • Find the platform.txt file,
    • AVR Architecture => {set path} \ hardware \ arduino \ avr \ platform.txt
    • SAM architecture => {set path} \ hardware \ arduino \ sam \ platform.txt
  • Inside this file you can change any flag , for example,
    • compiler.c.flags to change the default compilation flags for C ++ files.
    • compiler.cpp.flags to change the default compilation flags for C ++ files.
  • You can also change any of the “recipes” or compile the patterns in the corresponding section of the configuration file under the heading “AVR / SAM Compilation Templates”.
  • After making the changes, you should restart the Arduino IDE , at least in version 1.5.7.

For example,

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

It is expected that C ++ 11 will be enabled by default in the near future on the Arduino IDE. However, since version 1.5.8 (October 2014), this is still not the case.

+32
Oct 03 '14 at 8:21
source share

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

+15
Nov 15 '15 at 9:05
source share

Firstly, only gcc 4.7 and higher (and therefore avr-gcc 4.7 and higher) support C++11 . So check out the versions installed with:

 gcc --version avr-gcc --version 

If avr-gcc is 4.7 or higher, you can use C++11 .

Arduino IDE does not support special compiler flags. This is requested but not yet implemented.

Thus, you just have to use other environments or compile your program directly from the command line.

If compiling directly from the command line using avr-gcc you just need to add an additional compiler flag to enable C ++ 11 support.

 -std=c++11 

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




C++0x was the name of a working draft of the C++11 standard. C++0x gcc 4.3 support available. However, this is strictly experimental support, so you cannot expect the expected C++11 features. Here is a complete list of features available with the corresponding version of gcc . The availability of functions in avr-gcc will be the same as in the corresponding version of gcc .

Compiler flag for C++0x :

 -std=c++0x 
+14
Apr 12 '13 at 16:56
source share

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

As a hack, you can use a small proxy program (it must 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 internal array to execvp const char **command = &aptrs[0]; return execvp(command[0], command); } 
  • Compile the program.
  • Rename the original avr-g ++ exe to avr-g ++. orig.exe (or any other name).
  • Create the avr-g ++ file. ini, where the first line is the full path to the source program (for example, D: \ Arduino \ hardware \ tools \ avr \ bin \ avr-g ++. orig.exe) and add additional parameters, one per line, if desired.

You are done!

Avr-g ++ example. ini:

 D:\Arduino\hardware\tools\avr\bin\avr-g++.orig.exe -std=c++0x 

Hope this helps!

+1
Feb 01 '14 at 23:53
source share

I am using Ino and this worked:

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

This created a hex file of at least 15k size (with optimizations enabled) compared to about 5k for a standard build, which is a consideration for the poor small Atmega328. Perhaps for one of the microcontrollers there will be a lot of free space.

+1
Jul 02 '14 at 15:13
source share



All Articles