When can compiling c++ without RTTI cause problems?
Asked Answered
F

3

69

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?

Finned answered 20/12, 2010 at 3:15 Comment(4)
Is this a hypothetical question, or are you having problems and wondering if having RTTI disabled is causing them? If you're having problems, what sort of problems are you having?Hynda
I like this as a general (hypothetical) question. If there is some benefit in the flag, and one is not using type information, why shouldn't one include the flag?Primero
Why shouldn't you? The law of least surprise. One day someone is going to be very surprised that using a standard language feature breaks the build.Cloak
I was programming in a very memory constrained environment (small arm system) when I asked this question, so disabling RTTI was a useful gain.Finned
C
50

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.

Cloak answered 20/12, 2010 at 3:42 Comment(8)
You only ever need to use -fno-rtti when writing in a really memory-constrained environment, e.g. embedded programs; modern mobile phones usually have enough memory for you not to really need -fno-rtti, so you must be using something even much smaller.Organon
@Lie Ryan: Clang/LLVM disables RTTI in their project to scratch memory, yet it's meant to be run on regular computers. I admit it isn't a typical project though :)Sandalwood
what worries me is that disabling RTTI will not cause a compile error if it finds a dynamic_cast somewhereGum
@lurscher, I just checked with g++ 4.2.1 and it does generate an error. I get the following error "fnortti.cc:6: error: ‘dynamic_cast’ not permitted with -fno-rtti".Cloak
This is an old comment thread, but I came to it looking for background to understand this paper, stroustrup.com/OOPSLA-typeswitch-draft.pdf . The paper answers the question, as has been mentioned in these comments, typeid, dynamic_cast, and exception handling are the three situations that require RTTI.Cervin
Will this flag create problems for virtual functions?Puissance
@Puissance virtual functions should be fine without RTTI. I've linked another question that has a more detailed answer specific to that topic.Cloak
"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." gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/…Microreader
F
8

We have used gcc without rtti for 5 years with no specific problems (not using dynamic_cast or typeid)

Feucht answered 9/2, 2011 at 19:7 Comment(3)
and mostly without exceptions if no-rtti.Transpacific
@Amos: how are exceptions tied to RTTI?Libbylibeccio
@VioletGiraffe They aren't. GCC documentation states that exceptions can work without RTTI - any necessary data is generated as needed.Rapier
A
7

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.

Aswarm answered 27/8, 2021 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.