How to use C++17 for ESP32-S3(Devkit(C)-1)

Dear experts,

I am going to use C++17 in my project for the new ESP32-S3(Flash:16/8 MB,RAM:8MB) and my operating system is : Windows 10 .
How am I supposed to do that?

I really appreciate your attention.

Why do you think that your compiler do not used it already?

The link
https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/cplusplus.html

That doesn't necessarily reflect the version of C++ used by default in the ESP32 Arduino Core.
I haven't check Core 3.x yet, but ESP32 Arduino Core 2.x is at C++ 2011.

The Espressif core seems to be using std=gnu++2b, whatever that is (C++23, apparently.)
See .../packages/esp32/tools/esp32-arduino-libs/idf-release_v5.1-442a798083/esp32/flags/
(where presumably esp32 gets replaced by whichever esp32 variant you're actually using. (sigh.) I would assume that you can change it there as well, if desired, and it doesn't result in compiler errors for the SDK or core.)

Be that as it may, when compiled with the ESP32 Arduino Core's default compiler:

void setup() {
  Serial.begin(115200);
  delay(2000);

  auto ver = __cplusplus;
  Serial.print("C++ Version: ");
  Serial.println(ver);
}

void loop() {
}

You get:

C++ Version: 201103

That's C++ 2011 per This Link.

Hi @quark_1. I'm going to ask you to provide the full verbose output from a compilation.


:exclamation: This procedure is not intended to solve the problem. The purpose is to gather more information.


Please do this:

  1. Select File > Preferences... (or Arduino IDE > Settings... for macOS users) from the Arduino IDE menus.
    The "Preferences" dialog will open.
  2. Check the box next to "Show verbose output during: ☐ compile" in the "Preferences" dialog.
  3. Click the "OK" button.
    The "Preferences" dialog will close.
  4. Select Sketch > Verify/Compile from the Arduino IDE menus.
  5. Wait for the compilation to finish.
  6. Right click on the black "Output" panel at the bottom of the Arduino IDE window.
    A context menu will open.
  7. Select Copy All from the menu.
  8. Open a forum reply here by clicking the "Reply" button.
  9. Click the <CODE/> icon on the post composer toolbar.
    This will add the forum's code block markup (```) to your reply to make sure the error messages are correctly formatted.
    Code block icon on toolbar
  10. Press the Ctrl+V keyboard shortcut (Command+V for macOS users).
    This will paste the compilation output into the code block.
  11. Move the cursor outside of the code block markup before you add any additional text to your reply.
  12. Click the "Reply" button to post the output.

In case the output is longer than the forum software will allow to be added to a post, you can instead save it to a .txt file and then attach that file to a reply here:

  1. Open any text editor program.
  2. Paste the copied output into the text editor.
  3. Save the file in .txt format.
  4. Open a forum reply here by clicking the "Reply" button.
  5. Click the "Upload" icon (Upload icon) on the post composer toolbar:
    Upload icon on toolbar
    The "Open" dialog will open.
  6. Select the .txt file you saved from the "Open" dialog.
  7. Click the "Open" button.
    The dialog will close.
  8. Click the "Reply" button to publish the post.

Alternatively, instead of using the "Upload" icon on the post composer toolbar as described in steps (5) - (7) above, you can simply drag and drop the .txt file onto the post composer field to attach it.

I get that with v2 of arduino-esp32, but with the recent v3.0.1

Compiling sketch...
~/.arduino15/packages/esp32/tools/esp-xs3/2302/bin/xtensa-esp32s3-elf-g++

yields

C++ Version: 202100

which is at least C++20

Cool. Like I said, I hadn't tested ESP32 Arduino Core 3.x yet.

FWIW, the latest Teensy Core is now at C++ 2017. Nice to see at least the 3rd party cores moving on from C++ 2011.

Hi gfvalvo,

Thanks for your reply . I tested my Arduino based on your recommendation and by
your codes . The result was : 201703 . So, should I be confident enough about using C++17 by default ?

As @westfw wrote ,It seems that some flags are still set for using C++11 ,although I'm not sure completely, cause I searched for the path of flags ,which he mentioned ,but couldn't find anything useful . The only path which seems reasonable is:

"C:...\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.14 \platform.txt"

and I think in "platform.txt", are some flags, related to C++11 .

I really appreciate your attention.

Regards,
quark_1

Hi westfw,

Thank you to reply to my question. Could you please read what I wrote recently to "gfvalvo" as my reply ? I really want to know your opinion .

Regards,
quark_1

This provides the information I was hoping to extract from the verbose compilation output I requested. You are using an outdated version 2.0.14 of the "esp32" boards platform, from before they updated to the C++23 standard. If you want to use that standard, just use the Arduino IDE Boards Manager to update to the latest version of the platform. You can follow the instructions here to do that:

https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html#installing-using-arduino-ide

Please note that, along with the update to the C++23 standard, some breaking changes were made in the latest versions of the "esp32" boards platform, which might cause some sketches or libraries to no longer compile. You can learn about those changes and the necessary porting for affected code from this document:

https://github.com/espressif/arduino-esp32/blob/master/docs/en/migration_guides/2.x_to_3.0.rst#migration-from-2x-to-30

@kenb4's Post Above indicates that the C++ version in ESP32 Arduino Core 3.0.1 is not C++ 23.

I think the reason GCC hasn't updated the value of the __cplusplus macro when the -std=gnu++2b flag is used is because the C++23 standard hasn't been released yet. So the -std=gnu++2b flag causes the compiler to use the implementation of the draft of that standard rather than the final published standard's specification:

https://gcc.gnu.org/projects/cxx-status.html#cxx23

GCC has experimental support for the next revision of the C++ standard, which is expected to be published in 2023.

C++23 features are available since GCC 11. To enable C++23 support, add the command-line parameter -std=c++2b to your g++ command line. Or, to enable GNU extensions in addition to C++23 features, add -std=gnu++2b.

Important: Because the ISO C++23 standard is still evolving, GCC's support is experimental. No attempt will be made to maintain backward compatibility with implementations of C++23 features that do not reflect the final standard.

I get the same 202100 value for the __cplusplus macro if I compile a program while passing the -std=gnu++2b flag directly to GCC 12.2.0 (the version used by version 3.0.1 of the "esp32" boards platform):

$ printf '#include <iostream>\nint main() {std::cout << __cplusplus;}' > /tmp/foo.cpp && g++ -std=gnu++2b /tmp/foo.cpp && ./a

202100

Hi @ptillisch ,

Thank you for your time and attention to reply to my questions. The only thing that matters for me is using :C++17 ,and NOT necessarily C++23 .

Can you confirm that my Arduino uses C++17 by default? Is there any setting(Flag) somewhere, which I need to change?(I prefer not to update my Arduino .)

Or, should I update it to latest version definitely?

Regards,
quark_1

Just write a short program that uses a feature that wasn't introduced until C++17 (eg. std::variant). If it works, you're good to go.

Not definitely.

Keeping your project dependencies updated is generally a good thing because it allows you to get access to the recent enhancements and bug fixes the developers have made to the project. However, it is something that should be done with caution since an update might introduce breaking changes that affect a dependent project that was working perfectly with the previous version.

As I mentioned in my previous reply, the likelihood of breakage is even higher when updating from a 2.x version of the "esp32" boards platform to the modern 3.x versions because the developers have intentionally introduced some breaking changes in the 3.0.0 release (the bump from 2.x to 3.x is a standard way to communicate this fact).

Since the version of the platform you have installed now already provides the C++ standard version you want, there is no need to update the platform purely for the sake of getting the C++17 standard alone. The discussion had evolved to the subject of C++23 and my reply was addressed to the possible desire to get access to that standard.


I find that developers tend to have one of two distinct philosophies regarding dependency updates:

  • Only update a dependency when it is absolutely necessary ("if it ain't broke, don't fix it")
  • Update the dependency if possible whenever a new version is released

I follow the second. The reason is that I find giant dependency updates to be quite intimidating. Facing a huge changeset from years of development when trying to evaluate the potential impacts of the update on my project and adjusting the project is very overwhelming. I suspect that the developers who follow the first philosophy don't even bother trying to evaluate the changeset. Conversely, incremental small updates are much more approachable to me.

Of course the sum total of the work of handling each small update individually is the same (or more due to overhead) as handling them all at once for the huge update, but psychological factor is the difference between finding the motivation to keep things updated vs. putting the work off in favor of other items on my to-do list.