lundi 25 juillet 2016

How do I get constexpr to work with Arduino 1.6.9 on OSX

Supposedly Arduino's IDE > 1.6.2 has C++11 support.

I have just freshly downloaded and run version 1.6.9 on OSX, but I cannot get this simple program to compile:

constexpr int get_five() { return 5; }

void setup() {
  Serial.begin(9600);
  Serial.println(get_five());
}

void loop() {
}

I receive this error when I try to build or upload:

sketch_jul25a:1: error: 'constexprint' does not name a type
 constexpr int get_five() { return 5; }
 ^
exit status 1
'constexprint' does not name a type

I've looked at this question and answer, but it is supposedly no longer applicable in 1.6.9 version of the IDE that I am using - error: 'constexpr' does not name a type m- arduino ide

I have dug into the temporary files that are output by the IDE when building, and it seems it is trying to automatically generate headers for functions (I assume for multi-file sketch support), and does the wrong thing when it encounters constexpr:

#include <Arduino.h>
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
constexprint get_five(); // **** <- This looks to be the culprit
#line 3 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
void setup();
#line 9 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
void loop();
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
constexpr int get_five() { return 5; }

void setup() {
  Serial.begin(9600);
  Serial.println(get_five());
}

void loop() {
}

Is this a bug in the Arduino IDE? Is it unique to OSX? Is there a workaround that allows constexpr to work?

In googling I have found that some Arduino libraries are using constexpr, so I am assuming it could be made to work in some cases.

Aucun commentaire:

Enregistrer un commentaire