37

Can anybody tell me how to activate RTTI in c++ when working on unix. I heard that it can be disabled and enabled. on my unix environment,how could i check whether RTTI is enabled or disabled?

I am using the aCC compiler on HPUX.

j0k
  • 21,914
  • 28
  • 75
  • 84
Vijay
  • 59,537
  • 86
  • 209
  • 308

7 Answers7

26

Are you using g++ or some other compiler?

In g++ RTTI is enabled by default IIRC, and you can disable it with -fno-rtti. To test whether it is active or not use dynamic_cast or typeid

UPDATE

I believe that HPUX's aCC/aC++ also has RTTI on by default, and I am unaware of a way to disable it. Check your man pages.

Community
  • 1
  • 1
vladr
  • 61,376
  • 17
  • 123
  • 127
21

gcc has it on by default. Check if typeid(foo).name() gives you something useful.

#include <iostream>
#include <typeinfo>

int main()
{
 std::cout << typeid(int).name() << std::endl;
 return 0;
}

Without RTTI you get something like:

foo.cpp:6: error: cannot use typeid with -fno-rtti
Eddy Pronk
  • 6,030
  • 5
  • 29
  • 53
8

According to the docs there is no option to turn it off. The only two bits of standard C++ that can be selectively disabled are "scope of variables in for loops" (-Wc,ansi_for_scope,off) and Argument-Dependent Lookup of names (-Wc,-koenig_lookup,off). There's no option similar to -Wc,-RTTI,off

MSalters
  • 159,923
  • 8
  • 140
  • 320
6

All modern C++ compilers I know (GCC, Intel, MSVC, SunStudio, aCC) have RTTI enabled by default, so unless you have any suspects that it may be disabled for some reason you may safely assume that RTTI in on.

Artyom
  • 30,091
  • 20
  • 121
  • 208
2

RTTI will be enabled or disabled when compiling your program via compiler options - it's not something enabled or disabled in the Unix environment globally. The easiest way to see if it's enabled by default for your compiler is to just try compiling some code using RTTI.

Options to enable/disable RTTI will be compiler specific - what compiler are you using?

RTTI support is on by default in GCC, the option -fno-rtti turns off support (in case you're using GCC and maybe someone's turned off RTTI in a makefile or something).

Michael Burr
  • 311,791
  • 49
  • 497
  • 724
1

Enabling and disabling RTTI must be a compiler specific setting. In order for the dynamic_cast<> operation, the typeid operator or exceptions to work in C++, RTTI must be enabled. If you can get the following code compiled, then you already have RTTI enabled (which most compilers including g++ do automatically):

#include <iostream>
#include <typeinfo>

class A
{
public:
  virtual ~A () { }
};

class B : public A
{
};

void rtti_test (A& a)
{
  try
    {
      B& b = dynamic_cast<B&> (a);
    } 
  catch (std::bad_cast)
    {
      std::cout << "Invalid cast.\n";
    }
  std::cout << "rtti is enabled in this compiler.\n";
}

int
main ()
{
  A *a1 = new B;
  rtti_test (*a1);  //valid cast
  A *a2 = new A;
  rtti_test (*a2);  //invalid cast
  return 0;
}
pablomtz
  • 119
  • 7
Vijay Mathew
  • 25,329
  • 3
  • 54
  • 90
  • what would happen if ii compile with -fno-rtti? Would the compiler complain of using virtuals? – Abhinav Nov 16 '12 at 05:38
  • Just for somebody who curious about relationship between exceptions and RTTI. Here is discussion of the topic: http://stackoverflow.com/questions/10320072/is-there-a-relation-between-rtti-and-exceptions Summarizing, exceptions do not need RTTI to be enabled. As it does not use it's special features like dynamic_cast and typeid. But seems like it still uses some run-time type information for matching exceptions. – Roman Hwang May 23 '13 at 07:31
0

In g++ you can test the __GXX_RTTI macro to see if RTTI is on in your code. As others have pointed out -no-rtti turns of RTTI in g++. I would bet both these things work in clang as well.

#ifdef __GXX_RTTI
  w = dynamic_cast<FooBah*>(FooFactory(TYPE, arg));
  if (w != NULL)
  {
    if (w->thing == OK)
      FeastOnOrangUtansAndFruitBatsAndBreakfastCereals();
  }
#endif

In newer C++ we will have access to feature testing macros __cpp_rtti and many others that will make tese things easier.

emsr
  • 13,551
  • 6
  • 45
  • 59