Friday, August 10, 2012

GCC macros for portable development

While GCC define a large number of macros only some of them are useful for portable development. The use of pre-processor macros is a well-known technique for selecting which portions of your code can only be compiled on a specific platform.

The most interesting macros are:

  • __GNUC__: always defined for GCC
  • __GNUG__: always defined for G++
  • WIN32: defined on Windows only

This code:

#ifdef __GNUC__
  #ifdef WIN32
    std::cout << "GCC on Windows\n";
    std::cout << "GCC not on Windows\n";


GCC on Windows

when built with MinGW on Windows and


GCC not on Windows

when built with GCC in other platforms.

Alternatively with MinGW you can also use __MINGW32_VERSION which is defined if _mingw.h is directly or indirectly (e.g. by including any standard header file) included.

No comments: