Home > Uncategorized > Compile 32bit C/C++ Application in 64bit Linux

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

October 8th, 2009 Leave a comment Go to 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>
main()
{
    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
C++
long: 8
long long: 8

[sudar@tstsrv12 tmp]$ ldd out64
        linux-vdso.so.1 =>  (0x00007fffc43fe000)
        libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x0000003e7be00000)
        libm.so.6 => /lib64/libm.so.6 (0x0000000000601000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x0000003e79e00000)
        libc.so.6 => /lib64/libc.so.6 (0x0000000000886000)
        /lib64/ld-linux-x86-64.so.2 (0x0000000000110000)

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

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

[sudar@tstsrv12 tmp]$ ldd out32
        linux-gate.so.1 =>  (0x00130000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00133000)
        libm.so.6 => /lib/libm.so.6 (0x00223000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x0024c000)
        libc.so.6 => /lib/libc.so.6 (0x0025a000)
        /lib/ld-linux.so.2 (0x00110000)

Tags:
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.