Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard way to determine at compile-time if system is 32 or 64 bit?

I need to set #ifdef - checks for conditional compile. I want to automate the process but cannot specify the target OS/machine. Is there some way that the pre-compiler can resolve whether it it is running on 32-bit or 64-bit?

(Explanation) I need to define a type that is 64 bits in size. On 64bit OS it is a long, on most others it is a long long.

I found this answer - is this the correct way to go?

[edit] a handy reference for compiler macros

like image 645
slashmais Avatar asked Jul 15 '11 11:07

slashmais


1 Answers

The only compile check you can do reliably would be sizeof(void*) == 8, true for x64 and false for x86. This is a constexpr and you can pass it to templates but you can forget using ifdef with it. There is no platform-independent way to know the address size of the target architecture (at pre-process time), you will need to ask your IDE for one. The Standard doesn't even have the concept of the address size.

like image 146
Puppy Avatar answered Feb 28 '23 10:02

Puppy