When can compiling c++ without RTTI cause problems?
I'm using gcc's -fno-rtti
flag to compile my C++ without runtime type information.
Assuming I'm not using dynamic_cast<>
or typeid()
, is there anything that could lead me to later problems?
Since your question is specific to GCC you should consult carefully the documentation for the version you are using. The documentation for GCC 4.5.2 says the following. Which from my reading would indicate that if you avoid dynamic_cast and typeid, you should be ok. That said, I have no personal experience with -fno-rtti. Perhaps you might like to elaborate on why you are using -fno-rtti.
-fno-rtti
Disable generation of information about every class with virtual functions for use by the C++ runtime type identification features (dynamic_cast
andtypeid
). If you don't use those parts of the language, you can save some space by using this flag. Note that exception handling uses the same information, but it will generate it as needed. Thedynamic_cast
operator can still be used for casts that do not require runtime type information, i.e. casts tovoid *
or to unambiguous base classes.
There is discussion about the relationship between virtual functions and RTTI available at No RTTI but still virtual methods. The short version is that virtual functions should be fine without RTTI.
We have used gcc without rtti for 5 years with no specific problems (not using dynamic_cast or typeid)
I appreciate this posting another answer to a very old question, but I came across it today and would like to point out that the answers given up until now are not accurate in all regards.
There is a potential issue that might occur if your code is using a library which itself uses RTTI information.
The following code is a simple example of this:
#include <stdio.h>
// Library header
struct A { virtual void fn(); };
struct B : A { virtual void fn(); };
void lib_fn(A* ptr);
// Library code
#ifdef LIB
void A::fn() { puts("a"); }
void B::fn() { puts("b"); }
void lib_fn(A* ptr)
{
if (B* b = dynamic_cast<B*>(ptr))
puts("successful dynamic_cast");
ptr->fn();
}
#endif
// Application code
#ifdef APP
struct C : B
{
virtual void fn()
{ puts("c"); }
};
int main()
{
C obj;
lib_fn(&obj);
return 0;
}
#endif
Here we have a library which provides a number of classes. It is compiled with RTTI because it uses dynamic_cast
internally, but we are not aware of this (we haven't read the source code, and it wasn't mentioned in the library documentation). The following command can be used to create the library:
g++ -shared -o libmodule.so -fPIC code.cpp -DLIB
We also have our application. We subclass a library class and pass an instance of this into a library function. We compile our application without RTTI since we want to save space and we don't like the fact that RTTI leaches information about the names of our classes in our application binary. To create the application executable that uses the library:
g++ -s -fno-rtti -o app code.cpp -DAPP -L. -lmodule -Wl,-rpath,.
This application, compiled with the -fno-rtti
will crash since the application does not include RTTI information and even though it does not itself use it, the library does.
If this is a third-party library it may not be possible to be sure that it doesn't use dynamic_cast in a way that might affect your code. Worse, it might not today, but it might be replaced in future with a version that does, at which point the application will then start to crash.