How to use C++ 17 with ESP8266?

I am trying to use some features from C++ 17, but I am not able to make the compiler working. The compiler does not recognize the directives I use to change the version of C++:

xtensa-lx106-elf-g++: error: unrecognized command line option ‘-std=c++17’

Here is my platform.ini:

[env:esp01_1m]
platform = espressif8266
board = esp01_1m
framework = arduino
build_flags = -std=c++17
build_unflags = -std=gnu++11

I tried some other solutions from topics here in the community, but none worked.

Have anyone managed to get working with the board I am using?

Default is xtensa GCC 4.8.2, which doesn’t support C++17. Add the lines

platform_packages = 
    toolchain-xtensa@~2.100100.0

to your platformio.ini for GCC 10.1.0 and retry.

This is pretty identical to what was already discussed here at Custom framework and toolset for Arduino ESP8266 3.0.0 - #2 by maxgerhardt.

1 Like

Thank you Max! That seems to be working! However, now I get quite few warnings while compiling. I am not sure why, but they are all inside esp8266 Arduino packages. Why is this happening after using the new toolchain? Or at least how I can easily get rid only of these warnings?

Yet another point, even after adding the line to use the new toolchain, I still see "cppStandard": "c++11" inside c_cpp_properties.json file. Should not this be automatically changed? This makes me confused.

Ah, indeed, something is still wrong. The C++ version isn’t being applied actually.

The build_unflags must be -std=c++11 because that’s what it builds for

As soon as you do that however, it really applies C++17 and the current stable Arduino-ESP8266 fails to compile.

So we must use the newest version from the git by doing

[env:esp01_1m]
platform = espressif8266
board = esp01_1m
framework = arduino
platform_packages = 
    toolchain-xtensa@~2.100100.0
    framework-arduinoespressif8266 @ https://github.com/esp8266/Arduino.git

The build_flags and build_unflags are removed here when using the newest core because the standard version already is

But then a compile error comes up

C:\Users\Max\.platformio\packages\framework-arduinoespressif8266@src-31d658a59f41540201fc3726a1394910\cores\esp8266\time.cpp:27:14: fatal error: sys/_tz_structs.h: No such file or directory
Compiling .pio\build\esp01_1m\FrameworkArduino\umm_malloc\umm_malloc.cpp.o
   27 |     #include <sys/_tz_structs.h>
      |              ^~~~~~~~~~~~~~~~~~~
compilation terminated.

This issue is known.

You need to then Ctrl+Click on the time.cpp file in the output and comment out the offending line 27.

After a build, it will then go through with 0 warnings and C++17 actually enabled.

Linking .pio\build\esp01_1m\firmware.elf
Retrieving maximum program size .pio\build\esp01_1m\firmware.elf
Building .pio\build\esp01_1m\firmware.bin
Checking size .pio\build\esp01_1m\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [===       ]  33.6% (used 27536 bytes from 81920 bytes)
Flash: [===       ]  33.9% (used 258576 bytes from 761840 bytes)
Creating BIN file ".pio\build\esp01_1m\firmware.bin" using "C:\Users\Max\.platformio\packages\framework-arduinoespressif8266@src-31d658a59f41540201fc3726a1394910\bootloaders\eboot\eboot.elf" and ".pio\build\esp01_1m\firmware.elf"
==================== [SUCCESS] Took 5.11 seconds ====================

Caused by the issue above. When fixed, will show

            "cppStandard": "c++17",
1 Like

Thanks a lot it works now! I will mark the question as answered. I am just curious to know -if you have any idea- why isn’t C++17 the default at the moment? Is this because this error (the line we need to comment out) in Arduino? Or do you know if this gonna be the default any soon?

I do not like using framework-arduinoespressif8266@https://github.com/esp8266/Arduino.git without specifying a version, because they may break anytime, or what do you think?

Cheers.

1 Like

The used C++ version is bound to what the people at Arduino-ESP8266 decide to use for the core. And that was last upgraded to Gnu++11 in 2.6.0. PlatformIO must use the same if it wants to stay compatible with how the core is supposed to be compiled (as seen we also get errors if we just try and compile it with C++17) and is compiled in the Arduino IDE. PlatformIO currently uses the latest Arduino-ESP8266 release 2.7.4, which is still at Gnu++11 (Releases · esp8266/Arduino · GitHub). Since the master branch is now at C++17 I’d expect the next release to switch to C++17 and then PIO will pick it up in its normal update cycle.

That’s why we can append a tag / commit hash at the end. See docs (syntax is same). So you can add #20413f817b6456490779a04824cc302e0a5c9779 at the end of the link to fixate the version.

1 Like

FYI, issues time.cpp:27:14: fatal error: sys/_tz_structs.h: No such file or directory · Issue #7792 · esp8266/Arduino · GitHub and the PlatformIO related issues were opened; By either using the very-newest 10.2.0 (not 10.1.0) from a direct download link, one doesn’t even have to modify the time.cpp file anymore. Update request in platform-espressif8266 was also created.

1 Like