Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a GCC preprocessor directive to check if the code is being compiled on a 64 bit machine?

Tags:

c++

gcc

I am trying to do something like the following;

#ifdef 64-bit
    #define DECIMAL_FORMAT %ld
#else
    #define DECIMAL_FORMAT %d
#endif
.
intptr_t d;  
.
printf(“Some message with DECIMAL_FORMAT in the middle of it\n”, d);

The variable 'd' being of the type 'intptr_t' needs '%d' format specifier on 32 bit machines and format specifier '%ld' on 64 bit machines. I am looking a solution to be able to compile the code on both 32 bit machines and 64 bit machines without making changes to the GCC command line or the source code.

like image 506
Vilmorin Avatar asked Mar 25 '09 18:03

Vilmorin


1 Answers

I think __LP64__ might be what you're looking for. See http://gcc.gnu.org/onlinedocs/gcc-4.1.2/cpp/Common-Predefined-Macros.html

Maybe a better way to go, though, is to use the %p specifier to printf() instead of %ld or %d. Then you don't even have to worry what your pointer size is.

like image 71
Fred Larson Avatar answered Oct 15 '22 15:10

Fred Larson