C++17 support for Arduino ESP32

After the last update, I’ve found that C++17 now seems to be supported for Arduino at ESP-8266 with build_unflags = -std=gnu++11 and build_flags = -std=c++17

At least, I can use features with deconstruction and namespaces:

namespace A::B::C
{
  void TestCpp17()
  {
      auto p = std::pair<int, int>(1, 2);
      auto [a, b] = p;
      Serial.printf("OK: [%d, %d]", a, b);
  }
} // namespace
using namespace A::B::C;

Configuration from build log:

PLATFORM: Espressif 8266 (3.2.0) > WeMos D1 R2 and mini
HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash
PACKAGES:
 - framework-arduinoespressif8266 3.30002.0 (3.0.2)
 - tool-esptool 1.413.0 (4.13)
 - tool-esptoolpy 1.30000.201119 (3.0.0)
 - toolchain-xtensa 2.100300.210717 (10.3.0)

But, unfortunately, I couldn’t achieve the same result for ESP32 in such a simple way with build flags, new syntax features don’t work with errors:

src\main.cpp:4:12: error: expected '{' before '::' token
 namespace A::B::C
            ^
src\main.cpp:4:14: error: 'B' in namespace '::' does not name a type
 namespace A::B::C
              ^
src\main.cpp:16:20: error: 'A::B' has not been declared
 using namespace A::B::C;
                    ^
src\main.cpp:16:23: error: 'C' is not a namespace-name
 using namespace A::B::C;
                       ^
src\main.cpp:16:24: error: expected namespace-name before ';' token
 using namespace A::B::C;
                        ^
src\main.cpp: In function 'void A::setup()':
src\main.cpp:26:15: error: 'TestCpp17' was not declared in this scope
     TestCpp17();
               ^
src\main.cpp: At global scope:
src\main.cpp:31:1: error: expected '}' at end of input
 }
 ^
*** [.pio\build\ESP-32-Dev\src\main.cpp.o] Error 1

Configuration from build log:

CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 (3.3.2) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)      
PACKAGES:
 - framework-arduinoespressif32 3.10006.210326 (1.0.6)
 - tool-esptoolpy 1.30100.210531 (3.1.0)
 - toolchain-xtensa32 2.50200.97 (5.2.0)

So, is it now possible to use C++17 somehow for Arduino and ESP32 or still not yet?

Duplicate of Possible to use C++17. Together with the info in Missing branch #idf-release/v4.0 · Issue #4998 · espressif/arduino-esp32 · GitHub due to Arduino-ESP32 deleting the idf-release/v4.0 branch, one can deduce that

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
build_unflags = -std=gnu++11
build_flags = -std=c++17
; GCC 8.4.0 plus
; Arduino-ESP32 that does not shit
; itself when compiling with C++17
platform_packages =
   toolchain-xtensa32@~2.80400.0
   framework-arduinoespressif32 @ https://github.com/marcovannoord/arduino-esp32.git#idf-release/v4.0
monitor_speed = 115200

is the most minimal way of compiling

#include <Arduino.h>

namespace A::B::C
{
  void TestCpp17()
  {
      auto p = std::pair<int, int>(1, 2);
      auto [a, b] = p;
      Serial.printf("OK: [%d, %d]", a, b);
  }
} // namespace
using namespace A::B::C;

void setup(){
  Serial.begin(115200);
  TestCpp17();
}
void loop(){}
   framework-arduinoespressif32 @ https://github.com/marcovannoord/arduino-esp32.git#idf-release/v4.0

successfully.

>pio run -t upload -t monitor
...
RAM:   [          ]   4.2% (used 13624 bytes from 327680 bytes)
Flash: [==        ]  16.1% (used 211462 bytes from 1310720 bytes)
Building .pio\build\esp32dev\firmware.bin
..
--- Miniterm on COM4  115200,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun  8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1364
load:0x40078000,len:12656
load:0x40080400,len:3512
entry 0x40080624
OK: [1, 2]
--- exit ---

Note that it’ll probably also be possible to compile the new Arduino-ESP32 2.0.0 core version with C++17, however, since Support for the latest Arduino v2.0 · Issue #619 · platformio/platform-espressif32 · GitHub is not resolved yet, it can’t be used with PlatformIO. The previous stable core (1.0.6) does not compile with C++17, the idf-release/v4.0 version does, but it’s of course not the latest 2.0.0 one.

1 Like

Thank you, @maxgerhardt!
It works!
I’ve read some of those discussions but didn’t manage on my own to gather all the information into the working configuration.

Update for everybody who cares.
Espressif published idf-release/v4.4 branch in their repo:

And it seems that C++17 is supported now at least without foreign repos:

[env:Pure-ESP32-DevKit]
platform = https://github.com/platformio/platform-espressif32.git#feature/arduino-upstream
platform_packages =
    toolchain-xtensa32@~3.80200.200512
    framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#idf-release/v4.4

It’s said that IDF4.4 branch is supported:

Although, I didn’t try yet compiling all my builds yet…

I’m trying to use C++17 std::variant which I’m not able to get working after adding these platform_packages. Would I need to do something extra to be able to get the std::variant feature?

The information in this thread is outdated, please see ESP32 C++17 toolchain missing std::optional - #7 by maxgerhardt.

When I try the steps described above I get this error:

error: static assertion failed: portGET_ARGUMENT_COUNT() result does not match for 0 arguments
_Static_assert(portGET_ARGUMENT_COUNT() == 0, “portGET_ARGUMENT_COUNT() result does not match for 0 arguments”);

Is there any solution this problem?

Please post the exact plaformio.ini you’re having problems with. I’ve seen that error when people use c++17 and not gnu++17.

My platformio.ini

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200

build_unflags = -std=gnu++11
build_flags = -std=c++17

lib_deps =
ottowinter/ESPAsyncWebServer-esphome@^3.0.0
esphome/AsyncTCP-esphome@^2.0.0
moononournation/GFX Library for Arduino@^1.3.1

lib_ldf_mode=deep

platform_packages =
toolchain-xtensa32@~3.80200.200512
framework-arduinoespressif32 @ GitHub - espressif/arduino-esp32: Arduino core for the ESP32

As I said, c++17 is wrong because code relies on GNU extensions, so use gnu++17 instead. You also don’t need any of the plarform_packages, so delete those.

2 Likes

Thank you @maxgerhardt, Appreciate your help