How can I differentiate C and C++ compiler?

John (Eljay) Love-Jensen eljay@adobe.com
Thu Oct 8 14:53:00 GMT 2009


Hi Dave,

Here are some defines you could test against which show up for C++ and are absent for C:

__GNUG__
__GXX_WEAK__
__cplusplus

Note that the gcc compiler can compile either C or C++ code.  And the g++ compiler can compile either C or C++ code.  (As I understand it, neither gcc or g++ is a compiler per se.  Rather, they are toolchain drivers, which invoke the compiler.)

You can also explicitly tell the toolchain driver that the code is C or C++, using -x c and -x c++ (amongst others, see 'man gcc'), and which particular standard to adhere to by -std=<standard> (see 'man gcc').

To view the predefined preprocessor defines that are provided, for example:

echo '' | g++ -std=c++98 -ansi -pedantic -E -dM -x c++ - | sort 

Change the parameters a bit as desired, and save the outputs, and diff them to see the deltas.

Sincerely,
--Eljay



More information about the Gcc-help mailing list