Let’s say I have main.cpp
that includes utils.h
from lib
directory:
lib/utils.h
:
#ifndef LIB_UTILS_H
#define LIB_UTILS_H
inline void util() {
throw 13;
}
#endif // LIB_UTILS_H
main.cpp
:
#include "utils.h"
int main() {
util();
return 0;
}
If I tell clang
to search in lib
using -I
it respects -fno-exceptions
:
% clang++ -I lib -fno-exceptions main.cpp
In file included from main.cpp:1:
lib/utils.h:5:3: error: cannot use 'throw' with exceptions disabled
throw 13;
^
1 error generated.
%
But if I use -isystem
instead of -I
it compiles successfully:
% clang++ -isystem lib -fno-exceptions main.cpp
%
And when I try running it, it throws and exception:
% ./a.out
terminate called after throwing an instance of 'int'
zsh: IOT instruction ./a.out
I checked with Clang 14 and 18, they behave the same. gcc correctly fails to compile in both cases.
Why is -fno-exceptions
ignored by Clang?