Specifically I'd like to know what, if any, guarantees are made by GCC about how code that throws exceptions behaves when linked against code compiled using -fno-exceptions
.
The GNU libstdc++
manual says the following here.
Before detailing the library support for
-fno-exceptions
, first a passing note on the things lost when this flag is used: it will break exceptions trying to pass through code compiled with-fno-exceptions
whether or not that code has anytry
orcatch
constructs. If you might have some code that throws, you shouldn't use-fno-exceptions
. If you have some code that usestry
orcatch
, you shouldn't use-fno-exceptions
.
This sounds like a statement along the lines of, "Thou shalt not...." I.e. undefined behavior.
On the other hand, my impression from this SO question is that everything is kosher as long as the code compiled with -fno-exceptions
doesn't throw
, try
, or catch
(obviously a compile-time error) and exceptions never propagate through the functions from this library. And it makes sense: why should the library compiled with -fno-exceptions
care if exceptions are thrown so long as they don't interact with its functions?
I did a little tinkering and found that if I compile a simple program using GCC 7.1.1 in which one source file is compiled with -fno-exceptions
and the other throws and catches an exception, everything compiles, links, and runs fine. But that doesn't mean this behavior is guaranteed; it could still be undefined.
My motivation in all of this is that I have a situation in which I'm linking my own application code against a library built with -fno-exceptions
and, depending on which function calls are made to said library, throwing an exception in my own code causes an immediate segfault even when that exception does not propagate through the library's functions. It smells like a bug in the library to me, but I thought maybe this was permitted when -fno-exceptions
was passed during compilation.
GCC's actual reference on code-generation flags mentions -fexceptions
relatively briefly and doesn't answer my question. Anyone know of another reference/have relevant experience?
Update: I rebuilt the library from source, this time with exception support turned on. The segfault persists! Time for a bug report.