Detecting 64bit compile in C

后端 未结 9 1195
醉酒成梦
醉酒成梦 2020-11-28 12:01

is there a C macro or some kind of way that i can check if my c program was compiled as 64bit or 32bit at compile time in C?

Compiler: GCC Operating systems that i n

相关标签:
9条回答
  • 2020-11-28 12:25

    A compiler and platform neutral solution would be this:

    // C
    #include <stdint.h>
    
    // C++
    #include <cstdint>
    
    #if INTPTR_MAX == INT64_MAX
    // 64-bit
    #elif INTPTR_MAX == INT32_MAX
    // 32-bit
    #else
    #error Unknown pointer size or missing size macros!
    #endif
    

    Avoid macros that start with one or more underscores. They are not standard and might be missing on your compiler/platform.

    0 讨论(0)
  • 2020-11-28 12:26

    Since you tagged this "gcc", try

    #if __x86_64__
    /* 64-bit */
    #endif
    
    0 讨论(0)
  • 2020-11-28 12:29

    Use this UINTPTR_MAX value to check build type.

    #include <stdio.h>
    #include <limits.h>
    
    #if UINTPTR_MAX == 0xffffffffffffffffULL               
    # define BUILD_64   1
    #endif
    
    int main(void) {
    
        #ifdef BUILD_64
        printf("Your Build is 64-bit\n");
    
        #else
        printf("Your Build is 32-bit\n");
    
        #endif
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题