Skip to content

Commit

Permalink
Merge branch 'contrib/github_pr_11025' into 'master'
Browse files Browse the repository at this point in the history
Enable support for C++23 in esp-idf (GitHub PR)

Closes IDFGH-9684

See merge request espressif/esp-idf!23144
  • Loading branch information
0xjakob committed Apr 19, 2023
2 parents 852c60f + 6991a92 commit d82eb69
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion components/esp_common/include/esp_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extern "C" {
* - MACRO_ARGS(__VA_ARGS__) if __VA_ARGS__ was not empty
* - MACRO_NO_ARGS() if __VA_ARGS__ was empty
*
* @note In the future, we want to switch to C++20. We also want to become compatible with clang. Hence, we provide two
* @note In the future, we want to become compatible with clang. Hence, we provide two
* versions of the following macros which are using variadic arguments. One is using the GNU extension ##__VA_ARGS__.
* The other is using the C++20 feature __VA_OPT__(,). This allows users to compile their code with standard C++20
* enabled instead of the GNU extension. Below C++20, we haven't found any good alternative to using ##__VA_ARGS__.
Expand Down
2 changes: 1 addition & 1 deletion components/log/include/esp_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ void esp_log_writev(esp_log_level_t level, const char* tag, const char* format,
/// Log at ``ESP_LOG_ERROR`` level. @see ``printf``,``ESP_LOGE``,``ESP_DRAM_LOGE``

/**
* In the future, we want to switch to C++20. We also want to become compatible with clang.
* In the future, we want to become compatible with clang.
* Hence, we provide two versions of the following macros which are using variadic arguments.
* The first one is using the GNU extension \#\#__VA_ARGS__. The second one is using the C++20 feature __VA_OPT__(,).
* This allows users to compile their code with standard C++20 enabled instead of the GNU extension.
Expand Down
4 changes: 2 additions & 2 deletions docs/en/api-guides/cplusplus.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ esp-idf-cxx Component
C++ language standard
---------------------

By default, ESP-IDF compiles C++ code with C++20 language standard with GNU extensions (``-std=gnu++20``).
By default, ESP-IDF compiles C++ code with C++23 language standard with GNU extensions (``-std=gnu++23``).

To compile the source code of a certain component using a different language standard, set the desired compiler flag in the component CMakeLists.txt file:

.. code-block:: cmake
idf_component_register( ... )
target_compile_options(${COMPONENT_LIB} PRIVATE -std=gnu++2b)
target_compile_options(${COMPONENT_LIB} PRIVATE -std=gnu++11)
Use ``PUBLIC`` instead of ``PRIVATE`` if the public header files of the component also need to be compiled with the same language standard.

Expand Down
9 changes: 7 additions & 2 deletions tools/cmake/build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ function(__build_set_lang_version)
# Use latest supported versions.
# Please update docs/en/api-guides/cplusplus.rst when changing this.
set(c_std gnu17)
set(cxx_std gnu++20)
if(NOT ${env_idf_toolchain} STREQUAL "clang")
set(cxx_std gnu++23)
else()
# TODO: IDF-7241 - remove the exception for clang
set(cxx_std gnu++20)
endif()
else()
enable_language(C CXX)
# Building for Linux target, fall back to an older version of the standard
Expand All @@ -149,7 +154,7 @@ function(__build_set_lang_version)
"${preferred_c_versions}. Please upgrade the host compiler.")
endif()

set(preferred_cxx_versions gnu++20 gnu++2a gnu++17 gnu++14)
set(preferred_cxx_versions gnu++23 gnu++20 gnu++2a gnu++17 gnu++14)
set(ver_found FALSE)
foreach(cxx_version ${preferred_cxx_versions})
check_cxx_compiler_flag("-std=${cxx_version}" ver_${cxx_version}_supported)
Expand Down

0 comments on commit d82eb69

Please sign in to comment.