diff --git a/Makefile.dep b/Makefile.dep index 0b9f7cdc8d..346455bc38 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -617,6 +617,7 @@ ifneq (,$(filter cpp11-compat,$(USEMODULE))) USEMODULE += xtimer USEMODULE += timex FEATURES_REQUIRED += cpp + FEATURES_REQUIRED += libstdcpp endif ifneq (,$(filter gnrc,$(USEMODULE))) @@ -1085,6 +1086,11 @@ FEATURES_REQUIRED += $(filter cpu_core_%,$(FEATURES_PROVIDED)) # don't use idle thread if architecture has needed support FEATURES_OPTIONAL += no_idle_thread +ifneq (,$(filter libstdcpp,$(FEATURES_USED))) + # Also use C++ if libstdc++ is used + FEATURES_REQUIRED += cpp +endif + ifneq (,$(filter ecc_%,$(USEMODULE))) USEMODULE += ecc endif diff --git a/cpu/arm7_common/Makefile.features b/cpu/arm7_common/Makefile.features index 5183414c71..50167f4b86 100644 --- a/cpu/arm7_common/Makefile.features +++ b/cpu/arm7_common/Makefile.features @@ -2,6 +2,7 @@ FEATURES_PROVIDED += arch_32bit FEATURES_PROVIDED += arch_arm FEATURES_PROVIDED += arch_arm7 FEATURES_PROVIDED += cpp +FEATURES_PROVIDED += libstdcpp FEATURES_PROVIDED += periph_pm FEATURES_PROVIDED += puf_sram FEATURES_PROVIDED += ssp diff --git a/cpu/cortexm_common/Kconfig b/cpu/cortexm_common/Kconfig index 271d23c0ed..b7de561b76 100644 --- a/cpu/cortexm_common/Kconfig +++ b/cpu/cortexm_common/Kconfig @@ -31,6 +31,7 @@ config CPU_CORE_CORTEX_M select HAS_CPU_CORE_CORTEXM select HAS_PERIPH_PM select HAS_CPP + select HAS_LIBSTDCPP select HAS_CPU_CHECK_ADDRESS select HAS_SSP select HAS_CORTEXM_SVC diff --git a/cpu/cortexm_common/Makefile.features b/cpu/cortexm_common/Makefile.features index eef3d0ea8e..65e73500bb 100644 --- a/cpu/cortexm_common/Makefile.features +++ b/cpu/cortexm_common/Makefile.features @@ -1,10 +1,11 @@ FEATURES_PROVIDED += arch_32bit FEATURES_PROVIDED += arch_arm -FEATURES_PROVIDED += cpu_core_cortexm -FEATURES_PROVIDED += periph_pm FEATURES_PROVIDED += cortexm_svc FEATURES_PROVIDED += cpp FEATURES_PROVIDED += cpu_check_address +FEATURES_PROVIDED += cpu_core_cortexm +FEATURES_PROVIDED += libstdcpp +FEATURES_PROVIDED += periph_pm FEATURES_PROVIDED += ssp # cortex-m4f and cortex-m7 provide FPU support diff --git a/cpu/esp_common/Kconfig b/cpu/esp_common/Kconfig index c4d64e45be..5aa7ead0a3 100644 --- a/cpu/esp_common/Kconfig +++ b/cpu/esp_common/Kconfig @@ -15,6 +15,7 @@ config CPU_COMMON_ESP select HAS_ESP_NOW select HAS_ESP_SPIFFS select HAS_ESP_WIFI + select HAS_LIBSTDCPP select HAS_PERIPH_CPUID select HAS_PERIPH_HWRNG select HAS_PERIPH_PM diff --git a/cpu/esp_common/Makefile.features b/cpu/esp_common/Makefile.features index cbe20dc802..0020b7af35 100644 --- a/cpu/esp_common/Makefile.features +++ b/cpu/esp_common/Makefile.features @@ -8,6 +8,7 @@ FEATURES_PROVIDED += cpp FEATURES_PROVIDED += esp_now FEATURES_PROVIDED += esp_spiffs FEATURES_PROVIDED += esp_wifi +FEATURES_PROVIDED += libstdcpp FEATURES_PROVIDED += periph_cpuid FEATURES_PROVIDED += periph_hwrng FEATURES_PROVIDED += periph_pm diff --git a/cpu/fe310/Kconfig b/cpu/fe310/Kconfig index f3be4af51f..1335636419 100644 --- a/cpu/fe310/Kconfig +++ b/cpu/fe310/Kconfig @@ -24,6 +24,7 @@ config CPU_FAM_FE310 select HAS_PERIPH_PM select HAS_PERIPH_WDT select HAS_CPP + select HAS_LIBSTDCPP select HAS_SSP config CPU_MODEL_FE310_G000 diff --git a/cpu/fe310/Makefile.features b/cpu/fe310/Makefile.features index b439b69c3b..14b6ff70a2 100644 --- a/cpu/fe310/Makefile.features +++ b/cpu/fe310/Makefile.features @@ -1,6 +1,7 @@ FEATURES_PROVIDED += arch_32bit FEATURES_PROVIDED += arch_riscv FEATURES_PROVIDED += cpp +FEATURES_PROVIDED += libstdcpp FEATURES_PROVIDED += periph_cpuid FEATURES_PROVIDED += periph_gpio periph_gpio_irq FEATURES_PROVIDED += periph_pm diff --git a/cpu/mips32r2_common/Makefile.features b/cpu/mips32r2_common/Makefile.features index da355d8b12..bbd37b9eec 100644 --- a/cpu/mips32r2_common/Makefile.features +++ b/cpu/mips32r2_common/Makefile.features @@ -1,4 +1,5 @@ FEATURES_PROVIDED += arch_32bit FEATURES_PROVIDED += arch_mips32r2 FEATURES_PROVIDED += cpp +FEATURES_PROVIDED += libstdcpp FEATURES_PROVIDED += periph_pm diff --git a/cpu/native/Makefile.features b/cpu/native/Makefile.features index f1da4e3421..f278a2cee8 100644 --- a/cpu/native/Makefile.features +++ b/cpu/native/Makefile.features @@ -1,6 +1,7 @@ FEATURES_PROVIDED += arch_32bit FEATURES_PROVIDED += arch_native FEATURES_PROVIDED += cpp +FEATURES_PROVIDED += libstdcpp FEATURES_PROVIDED += periph_cpuid FEATURES_PROVIDED += periph_eeprom FEATURES_PROVIDED += periph_hwrng diff --git a/doc/doxygen/riot.doxyfile b/doc/doxygen/riot.doxyfile index 4e811ff167..892b6c1dbb 100644 --- a/doc/doxygen/riot.doxyfile +++ b/doc/doxygen/riot.doxyfile @@ -770,6 +770,7 @@ INPUT = ../../doc.txt \ ../../tests/README.md \ src/build-system-basics.md \ src/kconfig/kconfig.md \ + src/using-cpp.md \ src/advanced-build-system-tricks.md \ src/changelog.md \ ../../LOSTANDFOUND.md diff --git a/doc/doxygen/src/using-cpp.md b/doc/doxygen/src/using-cpp.md new file mode 100644 index 0000000000..844543feed --- /dev/null +++ b/doc/doxygen/src/using-cpp.md @@ -0,0 +1,50 @@ +Using C++ in RIOT {#using-cpp} +================= + +[TOC] + +Levels of Support {#levels-of-support} +================= + +A CPU in RIOT can have three levels of support for C++ code: + +1. No support for C++ at all +2. C++ is supported, but no libstdc++ implementation is available +3. C++ is supported and a libstdc++ implementation is available + +The reason for missing or only partial C++ support can be one (or more) of +the following: + +- No libstdc++ implementation for the target platform is available to RIOT, or + the official RIOT docker image is missing it +- Missing toolchain support for a given target platform +- The C++ toolchain requires library code (such as constructor guards for the + thread safe initialization of statically allocated instances) or hooks in + the startup process to perform initialization + +Using C++ +========= + +In order for C++ code to compile with RIOT, the following needs to be done: + +- All C++ files must have the file extension `.cpp`, all C++ headers `.hpp` + - For external code, overwriting the make variable `SRCXXEXT` e.g. to + `cxx` can be used to compile C++ files with other extensions, e.g. `.cxx` +- `FEATURES_REQUIRED += cpp` must be added to the applications `Makefile` + - If additionally the libstdc++ is used, `FEATURES_REQUIRED += libstdcpp` + must be used additionally + +RIOT Modules in C++ {#cpp-in-riot} +=================== + +RIOT modules should be written in C, so that boards/platforms without or partial +C++ support can still use these modules. However, external modules, packages, +and modules that require C++ support anyway (e.g. the Arduino compatibility +features) can be written in C++. These modules/packages have to depend on the +`cpp` feature (`FEATURES_REQUIRED += cpp`) and possibly the `libstdcpp` +feature using their `Makefile.dep`. + +See Also {#see-also} +======== + +@ref sys_c11_atomics_cpp_compat, @ref cpp11-compat diff --git a/kconfigs/Kconfig.features b/kconfigs/Kconfig.features index f86cd62df6..5bdb6f2a2f 100644 --- a/kconfigs/Kconfig.features +++ b/kconfigs/Kconfig.features @@ -67,6 +67,11 @@ config HAS_ETHERNET help Indicates that Ethernet connectivity is present. +config HAS_LIBSTDCPP + bool + help + Indicates that in addition to C++ support an libstdc++ is available. + config HAS_NO_IDLE_THREAD bool help diff --git a/tests/cpp_exclude/Makefile b/tests/cpp_exclude/Makefile index 93b4765299..5ae51e9253 100644 --- a/tests/cpp_exclude/Makefile +++ b/tests/cpp_exclude/Makefile @@ -1,6 +1,6 @@ include ../Makefile.tests_common -FEATURES_REQUIRED += cpp +FEATURES_REQUIRED += cpp libstdcpp USEMODULE += module_exclude EXTERNAL_MODULE_DIRS += $(CURDIR)/module_exclude diff --git a/tests/cpp_ext/Makefile b/tests/cpp_ext/Makefile index e4206736bd..0c0a24138e 100644 --- a/tests/cpp_ext/Makefile +++ b/tests/cpp_ext/Makefile @@ -1,6 +1,6 @@ include ../Makefile.tests_common -FEATURES_REQUIRED += cpp +FEATURES_REQUIRED += cpp libstdcpp USEMODULE += module EXTERNAL_MODULE_DIRS += $(CURDIR)/module