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

I need to install #ifdef - check conditional compilation. I want to automate the process, but I can not specify the target OS / machine. Is there a way that the pre-compiler can decide if it works on a 32-bit or 64-bit version?

(Explanation) I need to determine the type size of 64 bits. On a 64-bit OS, it is long; on most others, it is long.

I found this answer - is this the right way?

[edit] a convenient link for compiler macros

+6
source share
8 answers

The only compilation check you can do reliably will be sizeof(void*) == 8 , true for x64 and false for x86. This is constexpr, and you can pass it to templates, but you can forget to use ifdef. There is no platform independent way to find out the address size of the target architecture (during preprocessing), you need to ask about the IDE for one. The standard does not even have the concept of address size.

+10
source

There is no macro without standard language support to determine if a machine is 64-bit or 32-bit at the preprocessor stage.

+4
source

In response to your editing, there is a "macro less for you" way to get a type of 64 bits.

if you need a type that can contain 64 bits, then #include <cstdint> and use either int64_t or uint64_t . You can also use the standard Integer types provided by Boost .

Another option is to use long long . It is technically not part of the C ++ standard (it will be in C ++ 0x), but it is supported for almost every compiler.

+4
source
+2
source

Well, the answer will obviously be OS specific, so you need to narrow down your requirements.

For example, on Unix uname -a usually enough information is provided to distinguish between a 32-bit OS build and a 64-bit build.

The command may be called by your precompiler. Depending on its output, compiler flags may be set accordingly.

+1
source

I would look at the source code for the cross-platform library. This is a pretty big part. Each OS pair and compiler has its own set of definitions. A few libraries you can see:
http://www.libsdl.org/ \include\SDL_config*.h (multiple files)
http://qt.nokia.com/ \src\corelib\global\qglobal.h

+1
source

I will be tempted to pull the detection from the code and put it in the Makefile. Then you can use system tools to detect and install the appropriate macro, by which you switch your code.

In your makefile ...

 <do stuff to detect and set SUPPORT_XX_BIT to the appropriate value> gcc myFile.c -D$(SUPPORT_XX_BIT) -o myFile 

In your code ...

 #if defined(SUPPORT_32_BIT) ... #elif defined(SUPPORT_64_BIT) ... #else #error "Select either 32 or 64 bit option\n" #endif 
0
source

Probably the easiest way would be to compare the size of int and long long . You cannot do this in the preprocessor, but you can use it in static_assert .

Edit: Wow all negative voices. I made my point clearer. It also seems that I should have mentioned โ€œlong longโ€ rather than โ€œlongโ€ because of how MSVC works.

-2
source

Source: https://habr.com/ru/post/892821/


All Articles