tags:

views:

754

answers:

6

Hi,

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.

+8  A: 

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
+1  A: 

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
+4  A: 

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.

vladr
I am using aCC (ansi c) compiler for compiling my C++ programs.
Vijay Sarathi
A: 

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>

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);
  A *a2 = new A;
  rtti_test (*a2);
  return 0;
}
Vijay Mathew
+1  A: 

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
+2  A: 

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
+1 for actually digging up the HPUX docs
vladr