C++Builder Learn How To Check The 32bitness Or 64bitness Your C++ Application

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Learn How To Check The 32bitness Or 64bitness Your C++ Application
By Yilmaz Yoru March 7, 2021

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++ codes.

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 Для просмотра ссылки Войди или Зарегистрируйся to check platform operating system. All these macros are listed Для просмотра ссылки Войди или Зарегистрируйся. There are Для просмотра ссылки Войди или Зарегистрируйся Predefined Macros listed by LLVM.org Для просмотра ссылки Войди или Зарегистрируйся . 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
C++:
#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
C++:
#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
C++:
#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
Код:
#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
Код:
#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).
Код:
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 Для просмотра ссылки Войди или Зарегистрируйся to examine more options about OS & Compiler based options.