Archive for October 8th, 2009

Compile 32bit C/C++ Application in 64bit Linux

October 8th, 2009 No comments

To build (cross compilation) 32 bit C/C++ applications on 64 bit linux box, use “-m32” as a compiler argument.  You would need the following 32bit libraries in place.

  1. Install glibc.i386, glibc-devel.i386
  2. Install libgcc.i386
  3. Install libstdc++.i386

#include <cstdio>
#include <iostream>
    std::cout << “C++” << std::endl;
    printf ( “long: %d\n”, sizeof(long) );
    printf ( “long long: %d\n”, sizeof(long long) );
    return 0;

To compile in native 64bit:
[sudar@tstsrv12 tmp]$ g++ -m64 -o out64 code.cpp

[sudar@tstsrv12 tmp]$ ./out64
long: 8
long long: 8

[sudar@tstsrv12 tmp]$ ldd out64 =>  (0x00007fffc43fe000) => /usr/lib64/ (0x0000003e7be00000) => /lib64/ (0x0000000000601000) => /lib64/ (0x0000003e79e00000) => /lib64/ (0x0000000000886000)
        /lib64/ (0x0000000000110000)

To compile in 32bit:
[sudar@tstsrv12 tmp]$ g++ -m32 -o out32 code.cpp

[sudar@tstsrv12 tmp]$ ./out32
long: 4
long long: 8

[sudar@tstsrv12 tmp]$ ldd out32 =>  (0x00130000) => /usr/lib/ (0x00133000) => /lib/ (0x00223000) => /lib/ (0x0024c000) => /lib/ (0x0025a000)
        /lib/ (0x00110000)