问题
g++
is built using either the DWARF2
, sjlj
or seh
exception model. MinGW-builds provide various builds of g++
that have different exception models. I would like to be able to determine from the gcc
toolchain what exception model is being used. Is there a g++
argument that will dump the default exception model of the compiler?
回答1:
Edit: Originally, I was testing for the configuration flags that are described in g++ -v
. As Jonathon Wakely points out in the comments, this is not a good thing to do.
An inspection way to do it is to compile to assembly:
struct S { ~S(); };
void bar();
void foo() {
S s;
bar();
}
The result of g++ -S <filename> -o output.s
have the following exception references in them:
MinGW-4.8.1-x86-posix-sjlj
:
.def ___gxx_personality_sj0; .scl 2; .type 32; .endef
.def __Unwind_SjLj_Register; .scl 2; .type 32; .endef
.def __Unwind_SjLj_Unregister; .scl 2; .type 32; .endef
.def __Unwind_SjLj_Resume; .scl 2; .type 32; .endef
MinGW-4.8.1-x86-posix-dwarf
:
.def ___gxx_personality_v0; .scl 2; .type 32; .endef
.def __Unwind_Resume; .scl 2; .type 32; .endef
MinGW-4.8.1-x64-win32-sjlj
:
.def __gxx_personality_sj0; .scl 2; .type 32; .endef
.def _Unwind_SjLj_Register; .scl 2; .type 32; .endef
.def _Unwind_SjLj_Unregister; .scl 2; .type 32; .endef
.def _Unwind_SjLj_Resume; .scl 2; .type 32; .endef
MinGW-4.8.1-x64-posix-seh
:
.def __gxx_personality_seh0; .scl 2; .type 32; .endef
.def _Unwind_Resume; .scl 2; .type 32; .endef
MinGW-4.8.1-x64-posix-sjlj
:
.def __gxx_personality_sj0; .scl 2; .type 32; .endef
.def _Unwind_SjLj_Register; .scl 2; .type 32; .endef
.def _Unwind_SjLj_Unregister; .scl 2; .type 32; .endef
.def _Unwind_SjLj_Resume; .scl 2; .type 32; .endef
FC17-g++-4.7.2-x64
:
.cfi_personality 0x3,__gxx_personality_v0
.globl __gxx_personality_v0
call _Unwind_Resume
Looks like we should search for __gxx_personality_([a-z])(0-9]+)
and then compare the first capture group to:
v
=dwarf
seh
=seh
sj
=sjlj
回答2:
Just to complement the answers above, GCC has a predefined macro allowing to recognize at compile time whether SJLJ exception model is used:
__USING_SJLJ_EXCEPTIONS__
This macro is defined, with value 1, if the compiler uses the old mechanism based on setjmp and longjmp for exception handling.
See https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
According to documentation, it is available since at least version 3.1.1; I have just tested it on GCC 7.1 (under MinGW-w64).
来源:https://stackoverflow.com/questions/17967597/obtaining-current-gcc-exception-model