If you are looking to a way to reliably determine whether C++ code is being compiled in 32 bits or 64 bits there is way to check in C++ code.
For a cross platform compilation (cross compiler environments like FireMonkey framework in C++ Builder) there is no single reliable method to check if your application is running as a 32bits or 64bits application. Note that lower bits applications (i.e 32 bits) can run on higher bits operating systems, but you can not run higher bits applications in lower bits operating systems. Both _WIN32 and _WIN64 can sometimes both be undefined, if the project settings are wrong or missing. A project labelled “Win32” could be set to 64-bit, due to a project configuration error.
Sometimes (like happened before on Visual Studio 2008 SP1) intellisense / codeinsights does not grey out the correct parts of the code, according to the current #define which makes it difficult to see exactly which #define is being used at compile time.
You can use Predefined Macros to check platform operating system. All these macros are listed here. There are CLANG Predefined Macros listed by LLVM.org here . Here below we listed some #if / #endif examples. Note that #definitions are cared in completion, so if something is not related with under this #if clauses it is not compiled. For example if you check your application is for Windows or not, your Windows specific codes are only compiled if it is compiled for windows. So by using these kind of Predefined Macros, all codes will not be include in compilation that will let your compiled file size as much as small with predefinitions.
Checking Compilation if it is for 32bits or 64bits Windows
1 2 3 4 5 6 7 8 9 |
#if _WIN32 || _WIN64 #if _WIN64 // do things for 64bit windows #else // do things for 32bit windows #endif #endif |
Checking Compilation on Runtime if it is for 32bits or 64bits Windows
1 2 3 4 5 6 7 8 9 |
#if _WIN32 || _WIN64 #if _WIN64 std::cout << "Windows 64bits"; #else std::cout << "Windows 32bits"; #endif #endif |
Checking ARM Platform if it is 32bits or 64bits
1 2 3 4 5 6 7 8 9 10 |
#if __arm__ || __arm64__ // do ARM based specific things #if __arm__ // do things for ARM 32bits #elseif __arm64__ // do things for ARM 64bits #endif #endif |
Checking 64bit Compilation in GNU C Projects
1 2 3 4 5 6 7 8 9 |
#if __GNUC__ #if __x86_64__ || __ppc64__ || __arm64__ // do things for 64bits #else // do things for 32bits #endif #endif |
Checking 64bit Compilation on Runtime of GNU C Projects
1 2 3 4 5 6 7 8 9 |
#if __GNUC__ #if __x86_64__ || __ppc64__ || __arm64__ std::cout << "GNUC 64bits\n"; #else std::cout << " GNUC 32bits\n"; #endif #endif |
Checking 64bit Compilation with Void Pointer Size
This method may not work for all compilers, in some compiler options void size might be 4 bytes (32bits) while it is compiled for 64bits (8 bytes).
1 2 3 4 5 6 7 8 9 10 11 12 |
std:cout << "Running on " << 8*sizeof(void*) "bits OS\n"; if(sizeof(void*)==8) //8 bytes { std:cout << "Running 64bits\n"; } else { std:cout << "Running 32bits\n"; } |
Please see full list of Predefined Macros to examine more options about OS & Compiler based options.