Assume a simple file bla.cpp
:
struct MyClass {
virtual int foo(int x);
virtual ~MyClass();
};
int MyClass::foo(int x) { return x + 23; }
MyClass::~MyClass() {}
Build into a shared library with
g++ -c -fPIC bla.cpp
g++ -shared -o bla.so bla.o
will usually contain some type_info
symbol because RTTI is enabled by default on gcc. However, if I build with
g++ -c -fPIC -fno-rtti bla.cpp
the type_info
will be missing.
Is there a simple, reliable way (on gcc
or clang
) to check if a library has been built with -fno-rtti
or -frtti
? I ask because today I stared at the infamous undefined reference to type_info
and it took me a moment to understand that this was cause by a library I was linking against being built with -fno-rtti
.
If a class has virtual. functions, it should have type info. Do nm -C libname.so
and watch for "vtable for", "typeinfo for", and "typeinfo name for". Example:
00000000 b .bss
00000000 d .data
00000000 r .eh_frame
00000000 r .rdata$_ZTI3Foo
00000000 r .rdata$_ZTS3Foo
00000000 r .rdata$_ZTV3Foo
00000000 r .rdata$zzz
00000000 t .text
00000000 T Foo::foo()
00000000 R typeinfo for Foo
00000000 R typeinfo name for Foo
00000000 R vtable for Foo
U vtable for __cxxabiv1::__class_type_info
If you have vtable
but not typeinfo
, this is compiled with -fno-rtti
. Example:
00000000 b .bss
00000000 d .data
00000000 r .eh_frame
00000000 r .rdata$_ZTV3Foo
00000000 r .rdata$zzz
00000000 t .text
00000000 T Foo::foo()
00000000 R vtable for Foo
If you don't have any virtual functions, you cannot tell (and should not care).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With