This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Using C++ std::exceptions with NCS

Hi,

we're using a library which is written in C++ and is using exceptions. As stated here Zephyr has C++ support including exceptions.

Following the docs I set

...
CONFIG_CPLUSPLUS=y
CONFIG_LIB_CPLUSPLUS=y

CONFIG_NEWLIB_LIBC=y
CONFIG_EXCEPTIONS=y
CONFIG_NEWLIB_LIBC_NANO=n
...

But this leads to a linker error:

fini.c:(.text.__libc_fini_array+0x20): undefined reference to `_fini' 

When removing 

CONFIG_NEWLIB_LIBC_NANO=n

the code is being compiled with a warning that CONFIG_EXCEPTIONS is set to 'n' because of unsatisfied dependencies (!NEWLIB_LIBC_NANO). In this case thrown exceptions are not catched. Instead of running the catch-path the code is running into _exit in libc-hooks.c which is part of newlib. 

So, does anybody know how to enable the exception support? Am I missing some more CONFIGs?

Regards,
Oliver

Parents Reply Children
  • Hi Øyvind,

    thanks for your suggestion. It's definitely a good place to get Zephyr related help.

    For those that might run into the same issue, here's my solution:

    In prj.conf you need the C++ and exceptions related stuff:

    ...
    CONFIG_CPLUSPLUS=y
    CONFIG_LIB_CPLUSPLUS=y
    
    CONFIG_NEWLIB_LIBC=y
    CONFIG_EXCEPTIONS=y
    CONFIG_NEWLIB_LIBC_NANO=n
    ...

    To prevent the linker error, I had to implement following function:

    void exit(int reason) {
      printk("EXIT %d", reason);
      while (1)
        ;
    }

    Now the code compiles without error and the exceptions are being catched. But Interestingly exit() is being called. 

Related