Search code examples
c++cmakezephyr-rtos

Zephyr RTOS: Enabling C++ Exceptions


I'm trying to enable C++ Exceptions for Zephyr (Version 3.2.99). The documentation only states, that CONFIG_EXCEPTIONS must be enabled.

Where do I have to enable cpp exceptions, so that -fno-exceptions is not passed?

I enabled it in the CMakeLists.txt

cmake_minimum_required(VERSION 3.20.0)
set(BOARD xiao_ble)
find_package(Zephyr)
project(my_project)
# ...
target_sources(app PRIVATE
        src/main.cpp
# ...
        )
target_compile_options(app PRIVATE -fexceptions)

and in the KConfig (prj.conf):

# CPP
CONFIG_CPLUSPLUS=y
CONFIG_EXCEPTIONS=y
# ...

but still a -fno-exceptions is passed when building the project.


Solution

  • I didn't read the CMake output properly. The KConfig in the question produces the following warning: warning: EXCEPTIONS (defined at subsys/cpp/Kconfig:76) was assigned the value 'y' but got the value 'n'. Check these unsatisfied dependencies: LIB_CPLUSPLUS (=n).

    This is because enabling the CONFIG_EXCEPTIONS requires CONFIG_LIB_CPLUSPLUS to be enabled and CONFIG_NEWLIB_LIBC_NANO to be disabled, as stated in the KConfig options documentation. After adding these two configurations to the prj.conf file like this:

    # CPP
    CONFIG_CPLUSPLUS=y
    CONFIG_NEWLIB_LIBC_NANO=n
    CONFIG_LIB_CPLUSPLUS=y
    CONFIG_EXCEPTIONS=y
    # ... any other configurations
    

    exceptions can be thrown.