Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

conditional compilation

7 views
Skip to first unread message

ArbolOne

unread,
Aug 13, 2012, 11:33:48 AM8/13/12
to
I'd like to know how to tell C++ that if it is VC++ it should compiler this line or if it is GCC it should compile another line, i.e.
if GCC
#define FUNCTION __PRETTY_FUNCTION__
if MSVC
#define FUNCTION __func__

any body?

Jan Seiffert

unread,
Aug 13, 2012, 1:11:48 PM8/13/12
to
ArbolOne schrieb:
Most compiler contain an builtin define which identifies the compiler.
For GCC it is:
__GNUC__
i think for MS VC++ it is:
__MSVC__

So you can write:

#ifdef __GNUC__
# define FUNCTION __PRETTY_FUNCTION__
#elif defined(__MSVC__)
# define FUNCTION __func__
#else
# if (__STDC_VERSION__-0) >= 199901L
/* compiler is C99 compatible */
# define FUNCTION __func__
# else
# define FUNCTION "unknown_func"
# endif
#endif

Often compiler also contain builtin defines to ask for the compiler
version, if some magic you want to do is version specific.

As for the __GNUC__ define, you have to be a little bit careful.
Some non-GCC compiler now a days define it to say they are GCC compatible.
Unfortunately they are not 100% compatible, and i was badly burned by it
hitting some edge cases.
Clang is very notorious for this (all the new shiny Apple XCode foo),
the Sun Studio compiler also has some surprises in stock. Then you have to
exclude them by __clang__ or __SUNPRO_C.
It may not be a problem in the __func__/__PRETTY_FUNCTION__ case (may!),
but just as a general remainder.

Greetings
Jan
0 new messages