I installed the Espressif8266 platform on platformio to work on the Nodemcu board, but in my code I get some errors I believe due to that the installed toolchain uses gcc 4.8.2. I was wondering if there is an Espressif8266 toolchain using newer gcc 5+.
What errors exactly?
two errors:
1- “array used as initializer” in a class constructor. Googling this error I understood that this error is a bug that has been fixed by gcc5 and later versions. the source of the error is in-class initialization of char like “char pwd_r[10]=“12345”;”. for reference here is the link that explains the error:
2- in the following code
string x={‘a’,‘b’,‘c’,‘d’,‘\0’};
x.erase(5,1);
strlen(x.c_str);
the function strlen gives the following error “error: cannot convert ‘std::basic_string<_CharT, _Traits, _Alloc>::c_str<char, std::char_traits, std::allocator >’ from type ‘const char* (std::basic_string::)()const noexcept (true)’ to type ‘const char*’”
I don’t have a problem compiling your code when upgrading the C++ version to gnu11
.
platformio.ini
:
[env:esp8266]
platform = espressif8266
board = nodemcuv2
framework = arduino
upload_speed = 921600
build_unflags = -std=gnu99
build_flags = -std=gnu11
lib_ignore = StandardCplusplus
main.cpp
:
#include <Arduino.h>
#include <string>
#include <cstring>
void setup() {
Serial.begin(9600);
Serial.println("\nOriginal string:");
std::string x={'a','b','c','d','\0'};
Serial.println(x.c_str());
//erase the 'd'
x.erase(3,1);
Serial.println("Modified string: ");
Serial.println(x.c_str());
}
void loop() {
}
Prints
Original string:
abcd
Modified string:
abc