PlatformIO Community

How to positively detect if rtti is enabled with #if

PlatformIO IDE v 2.2.0, Core 3.6.7
VSCode v 1.35.1
Linux Mint v 17.2
gcc v 4:4.8.2-1ubuntu6

I want to display some info about a Class using typeid, and I know that requires having build_unflags = -fno-rtti in my platformio.ini, which I have. But I also want to not require that a user enable rtti, because of the additional memory it uses. So I want to do something like this:

#if <something that positively tells the compiler that rtti is enabled>
virtual const char* getClassName() { return typeid(*this).name(); } 

and use the same #if / #endif around the one line that actually displays the calls name in the Serial Monitor. But I’ve tried all kinds of things for that , and nothing seems to detect that rtti is enabled, because my method is never defined and my display code is never called.

Is there something that will tell the preprocessor that rtti is enabled, or not? Thanks!

After some quick googling I don’t think there’s a macro. You might get better help on this forum, Stackoverflow or their email lists.

Thanks, @maxgerhardt - I’ll try the gcc forum.

BTW, have you tried __GXX_RTTI macro? Something like:

#if defined(__GXX_RTTI)
    virtual const char* getClassName() { return typeid(*this).name(); } 
1 Like

@valeros, yes, someone on the gcc forum pointed me to that, and it works perfectly.

However, I used:
#if __GXX_RTTI
rather than
#ifdef __GXX_RTTI
something about it being defined, but not true? #ifdef might work, but I know that #if (and #if ! __GXX_RTTI) work.

Someone else suggested __cpp_rtti, which did NOT work. I think it’s because of the version of compiler I’m using.