help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: conditional compilation


From: Jan Seiffert
Subject: Re: conditional compilation
Date: Mon, 13 Aug 2012 19:11:48 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120729 Firefox/14.0.1 SeaMonkey/2.11

ArbolOne schrieb:
> 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?
> 

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]