c++gccrtti

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?


Solution

  • 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, as I understand it, means if you avoid dynamic_cast() and typeid(), you should be ok.

    -fno-rtti
    Disable generation of information about every class with virtual functions for use by the C++ runtime type identification features (dynamic_cast and typeid). 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. The dynamic_cast operator can still be used for casts that do not require runtime type information, i.e. casts to void * 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.

    As one of the comments to this answer rightly points out that mixing code compiled with different RTTI settings may not work. From more recent GCC documentation:

    Mixing code compiled with -frtti with that compiled with -fno-rtti may not work. For example, programs may fail to link if a class compiled with -fno-rtti is used as a base for a class compiled with -frtti.

    One way to avoid this problem is to ensure that all your code and dependencies are compiled with -fno-rtti. Please see the answer by Andy G for a more detailed discussion of this problem.