How to detect a 64-bit build on Linux using a preprocessor?

I'm going to port the Windows 32 Bit application to 64-bit, but may decide to port all this to Linux later.

The code contains sections depending on the amount of memory available for the application (which depends on whether I create an assembly of 32 or 64 bits), while the ability to compile a 32-bit version of the code must be preserved for backward compatibility.

On Windows, I can simply wrap the relevant sections of code in preprocessor statements to make sure that the correct version of the code is compiled.

Unfortunately, I have very little programming experience on the Linux platform, so the question arose:

How can I identify a 64-bit build on Linux?

Is there any (preferably not a compiler) preprocessor I can check this out?

Thanks in advance!

\ Bjorn

+3
source share
2 answers

Assuming you are using the recent GNU GCC compiler for IA32 (32-bit) and amd64 (64-bit without Itanium for AMD64 / x86-64 / EM64T / Intel 64), since few people need another compiler for Linux (Intel and PGI )

There is a compiler line switch (which you can add to CFLAGS in the Makefile) -m64 / -m32 to control the build target.

For conditional code C:

#if defined(__LP64__) || defined(_LP64)
#define BUILD_64   1
#endif

or

#include <limits.h>
#if ( __WORDSIZE == 64 )
#define BUILD_64   1
#endif

While the first one is GCC specific, the second one is more portable, but may be wrong in some strange environment that I can't think of.

IA-32/x86 (x86-32) x86-64/amd64. , IA-64 (Itanium).

. GCC, 64- GNU/Linux, 64- Linux .

+6

GCC:

__LP64__

_LP64

1, ( ) , int 64- int 32- .

, ?

,

#define __64BIT (__SIZEOF_POINTER__ == 8)
+5

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


All Articles