Thursday, April 8, 2010

Conditional Compilation Between GCC Versions

In assignment 4 of 379 I had to use the deprecated hash_map data structure. It doesn't compile on ohaton because the GCC version is 4.2.1. In that version hash_map is in the include directory

#include <ext/hash_map>

and on my home machine (GCC 4.1.1) it is in

#include <hash_map>

To get the source to compile on both machines I used the following precompiler directives:

#if ( __GNUC__ == 4 && ( __GNUC_MINOR__ == 2 ) )
#include <ext/hash_map>
#else
#include <hash_map>
#endif

Note that if you need finer granularity there is also a __GNUC_PATCHLEVEL__ . So GCC version 4.2.1: __GNUC__ == 4, __GNUC_MINOR__ == 2 and __GNUC_PATCHLEVEL__ == 1.


This technique can also be used:

#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 
+ __GNUC_PATCHLEVEL__) 
 ...
 /* Test for GCC > 3.2.0 */ 
#if GCC_VERSION > 30200

No comments:

Post a Comment

Post a Comment