2

I have a small C++ program that I need to use exceptions in. When I try to compile it I get the error:

error: exception handling disabled, use '-fexceptions' to enable

In my CMakeLists.txt I set the C++ version to 11 and enable -fexceptions

set(CMAKE_CXX_STANDARD 11)
...
target_compile_options (test PUBLIC -fexceptions)

Edit: for future readers this is for a Raspberry Pi Pico, and I found the answer.

Mr.Samson
  • 49
  • 1
  • 4
  • File a bug ticket then. If you do everything right (can't tell without [mcve]) and it doesn't work, it must be a bug. Seriausly, it's hard to tell with the little info you provide and just your interpretations. As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Feb 11 '21 at 20:33
  • 1
    In C++ exception handling should be enable by default. So if it isn't then there might be something else in your CMake project that disables it. Maybe another package you include. IIRC the AWS SDK for example caused such an issue. – t.niese Feb 11 '21 at 20:46

2 Answers2

2

After walking away from the problem for a while and coming back to it I found the answer. This was for a Raspberry Pi Pico; I didn't think that was important, but it turned out to be very important. Following the comment from t.niese I searched the sdk for -fno-exceptions and found in the sdk where exceptions were turned off. After reading the sdk documentation I added the line set(PICO_ENABLE_CXX_EXCEPTIONS 1) to my CMakeLists.txt file and that turned exceptions on.

Mr.Samson
  • 49
  • 1
  • 4
1

Like what Mr.Samson suggest, but I use "set(PICO_CXX_ENABLE_EXCEPTIONS 1)".

Horizon
  • 11
  • 2
  • It seems `PICO_ENABLE_CXX_EXCEPTIONS` in Mr.Samson's answer is a typo, whereas your CMake variable name is the only correct one. You could suggest an edit on that answer instead of a separate answer. Also, you can learn how to format your posts (and, particularly, code excerps) properly [here](https://stackoverflow.com/editing-help#code). – YurkoFlisk Jul 23 '22 at 17:05