WilliamKF
WilliamKF

Reputation: 43079

What are the gcc predefined macros for the compiler's version number?

I have run into a bug with gcc v3.4.4 and which to put an #ifdef in my code to work around the bug for only that version of the compiler.

What are the GCC compiler preprocessor predefined macros to detect the version number of the compiler?

Upvotes: 40

Views: 32982

Answers (5)

nos
nos

Reputation: 229048

There's 3 macros for the gcc version you can test on.

__GNUC_MINOR__ 
 __GNUC_PATCHLEVEL__ 
 __GNUC__ 

e.g. my gcc v 4.1.3 defines them as such:

#define __GNUC_MINOR__ 1
#define __GNUC_PATCHLEVEL__ 3
#define __GNUC__ 4

You can see the "buitin" macros defined by running

gcc -E -dM -x c /dev/null

Upvotes: 4

Alexis Wilke
Alexis Wilke

Reputation: 20725

As a side note, if you are using glib, then you have access to the following macro that can be used to check the GNU C/C++ compiler version:

#ifdef __GNUC__
#define G_GNUC_CHECK_VERSION(major, minor) \
    ((__GNUC__ > (major)) || \
     ((__GNUC__ == (major)) && \
      (__GNUC_MINOR__ >= (minor))))
#else
#define G_GNUC_CHECK_VERSION(major, minor) 0
#endif

Notice how the macro always returns false (0) when you are not compiling with the GNU compiler.


As a result, I use the following macro in my snapdev library version.h header. In my case, I wanted to also check the patch and I use many libraries, so I wrote a more generic version:

#define     SNAPDEV_CHECK_VERSION(wanted_major, wanted_minor, wanted_patch, current_major, current_minor, current_patch) \
                (((current_major) > (wanted_major)) || \
                    (((current_major) == (wanted_major)) \
                        && (((current_minor) > (wanted_minor)) || \
                            (((current_minor) == (wanted_minor)) \
                                && ((current_patch) >= (wanted_patch))))))

and I have a specialized check for GCC so you don't have to remember the name of the macros including the version:

#define     SNAPDEV_CHECK_GCC_VERSION(wanted_major, wanted_minor, wanted_patch) \
                SNAPDEV_CHECK_VERSION(wanted_major, wanted_minor, wanted_patch, \
                                      __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)

and as I do with much of my code, I wrote a unit test so I can say that it is unlikely that the test will fail unless you feed it invalid data (famous last words).

Upvotes: 0

DigitalRoss
DigitalRoss

Reputation: 146043

From the gnu cpp manual...


__GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__

These macros are defined by all GNU compilers that use the C preprocessor: C, C++, Objective-C and Fortran. Their values are the major version, minor version, and patch level of the compiler, as integer constants. For example, GCC 3.2.1 will define __GNUC__ to 3, __GNUC_MINOR__ to 2, and __GNUC_PATCHLEVEL__ to 1. These macros are also defined if you invoke the preprocessor directly.

__GNUC_PATCHLEVEL__ is new to GCC 3.0; it is also present in the widely-used development snapshots leading up to 3.0 (which identify themselves as GCC 2.96 or 2.97, depending on which snapshot you have).

If all you need to know is whether or not your program is being compiled by GCC, or a non-GCC compiler that claims to accept the GNU C dialects, you can simply test __GNUC__. If you need to write code which depends on a specific version, you must be more careful. Each time the minor version is increased, the patch level is reset to zero; each time the major version is increased (which happens rarely), the minor version and patch level are reset. If you wish to use the predefined macros directly in the conditional, you will need to write it like this:

          /* Test for GCC > 3.2.0 */
          #if __GNUC__ > 3 || \
              (__GNUC__ == 3 && (__GNUC_MINOR__ > 2 || \
                                 (__GNUC_MINOR__ == 2 && \
                                  __GNUC_PATCHLEVEL__ > 0)))

Upvotes: 52

Gregory Pakosz
Gregory Pakosz

Reputation: 70204

__GNUC__, __GNUC_MINOR__ and __GNUC_PATCHLEVEL__.

For instance, GCC 4.0.1 will do:

#define __GNUC__ 4
#define __GNUC_MINOR__ 0
#define __GNUC_PATCHLEVEL__ 1

Here is a little command line that is nice to remember when you are wondering which are the predefined preprocessor directives defined by the GNU GCC compiler under your current programming environment:

gcc -E -dM - < /dev/null |less

Upvotes: 22

From the online docs:

__GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__
These macros are defined by all GNU compilers that use the C preprocessor: C, C++, Objective-C and Fortran. Their values are the major version, minor version, and patch level of the compiler, as integer constants. For example, GCC 3.2.1 will define __GNUC__ to 3, __GNUC_MINOR__ to 2, and __GNUC_PATCHLEVEL__ to 1. These macros are also defined if you invoke the preprocessor directly.

and

__VERSION__
This macro expands to a string constant which describes the version of the compiler in use. You should not rely on its contents having any particular form, but it can be counted on to contain at least the release number.

Upvotes: 4

Related Questions