How to identify a 64 Bit build on Linux using the preprocessor?

13,638

Solution 1

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

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

For conditional C code:

#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 not be correct in some bizarre environment I cannot think of.

At present both should both work for IA-32 / x86 (x86-32) and x86-64 / amd64 environments correctly. I think they may work for IA-64 (Itanium) as well.

Also see Andreas Jaeger's paper from GCC Developer's Summit entitled, Porting to 64-bit GNU/Linux Systems which described 64-bit Linux environments in additional detail.

Solution 2

According to the GCC Manual:

__LP64__

_LP64

These macros are defined, with value 1, if (and only if) the compilation is for a target where long int and pointer both use 64-bits and int uses 32-bit.

That's what you need, right?

Also, you can try

#define __64BIT (__SIZEOF_POINTER__ == 8)
Share:
13,638
Kaushik Sarkar
Author by

Kaushik Sarkar

I am a Windows dev by trade, with some specialization in the integration of 3rd party media APIs, especially QuickTime and Windows Media.

Updated on June 04, 2022

Comments

  • Kaushik Sarkar
    Kaushik Sarkar almost 2 years

    I am about to port a Windows 32 Bit application to 64 Bit, but might decide to port the whole thing to Linux later.

    The code contains sections which are dependent on the amount of memory available to the application (which depends on whether I'm creating a 32 or 64 Bit build), while the ability to compile a 32 Bit version of the code should be preserved for backward compatibility.

    On Windows, I am able to simply wrap the respective code sections into preprocessor statements to ensure the right version of the code is compiled.

    Unfortunately I have very few experience on programming on the Linux platform, so the question occurred:

    How am I able to identify a 64 Bit build on the Linux platform?

    Is there any (preferably non-compiler-specific) preprocessor define I might check for this?

    Thanks in advance!

    \Bjoern

  • Chris Huang-Leaver
    Chris Huang-Leaver about 15 years
    It's the same with Solaris CC, _ILP32 is defined if this isn't. I took the time to look it up and DrJokepu beat me to it :-(
  • R.. GitHub STOP HELPING ICE
    R.. GitHub STOP HELPING ICE over 12 years
    There is a 100% portable alternative: #if UINTPTR_MAX == 0xffffffff (32bit) #else (assume 64bit or test other possibilities).
  • Ankit Roy
    Ankit Roy over 11 years
    Will you look at that -- an answer that mentions the actual documentation! :) +1.
  • Brett Hale
    Brett Hale over 8 years
    More specifically, uintptr_t is optional for C99. However, Linux appears to mandate the existence of this type, as well as: sizeof(void *) == sizeof(long)