Wednesday, 3 July 2019

How to compile 32 bit binary on 64 bit machine



GCC option to compile 32 bit binary on 64 bit machine:


Generate code for a 32-bit or 64-bit environment.
  • -m32 
    • sets "int", "long", and pointer types to 32 bits, and generates code that runs on any i386 system.
  • -m64
    •  sets "int" to 32 bits and "long" and pointer types to 64 bits, and generates code for the x86-64 architecture.
    • For Darwin only the -m64 option also turns off the -fno-pic and -mdynamic-no-pic options.
  • -mx32
    • sets "int", "long", and pointer types to 32 bits, and generates code for the x86-64 architecture.


Compile:

velrajk/sample$ gcc sizeof.c
velrajk/sample$ gcc -m32 sizeof.c -o 32_bit
velrajk/sample$

Output:

velrajk/sample$ ./32_bit
size of vel = 4 int= 4 float = 4double= 8 0 = 4 NULL = 4  "" = 1 int * = 4 unsigned long = 4 Unsigned int = 4
velrajk/sample$ ./a.out
size of vel = 4 int= 4 float = 4double= 8 0 = 4 NULL = 8  "" = 1 int * = 8 unsigned long = 8 Unsigned int = 4
velrajk/sample$


No comments:

Post a Comment