Compile and execute 32 bits application in 64 bits operating system

October 1st, 2009 mysurface Posted in Developer, gcc, Misc | Hits: 232196 | 3 Comments »

The world is going towards 64 bits machines and operating system for personal computer, but there are still some libs and software only support 32 bits. Therefore in this transition period, there is a needs to support both 64 bits and 32 bits applications.

Lately I been using x86_64 Fedora Linux. There are requirements for me execute some 32 bits binaries as well as compiling to produce 32 bits binaries.

First of all, I do not aware of any great different for me based on user experience while migrating from 32 bits to 64 bits Fedora. Maybe this is because the fedora repository is in the good shape, where everything that I had downloaded by using yum, will works perfectly.

Until one day I downloaded some legacy projects from the 3rd party website, that piece of software comes with 32 bits libs which needs 32 bits compilation. I started to realized that the binaries that I had compiled in my 64 bits environment is not working at 32 bits OS even though I am using the legacy version of gcc 3.4 compiler.

Fedora x86_64 allows you to run 32 bits software given that the dynamic libs is installed properly. I realized that my fedora does have two directories for lib, /lib and another one is /lib64. When I am searching for a particular lib using yum, let say libxml2, I realized that yum is actually returns me both 32 bits and 64 bits result.

yum search libxml2
libxml2.i386 : Library providing XML and HTML support
libxml2.x86_64 : Library providing XML and HTML support

By default if I execute “yum install libxml2”, yum will install the 64 bits one for me. In my case I want the 32 bits glibc, what I have to do is to specify the exact package name.

yum install libxml2.i386

After the installation, now I can run the 32 bits application that requires libxml2.

How about compiling 32 bits application in 64 bits operating system?

First of all, I need to make sure I had installed all the development packages of the 32 bits libs required. Let takes libxml2 as the example, I need to yum ‘libxml2-devel.i386’.

Next tell gcc that I am going to compile the codes into 32 bit binaries by specify the -m32 CFLAGS. Assume I wrote a simple hello.c.

gcc -m32 -o hello{,.c} -lxml2

Now I need to produce the binary that can be run under Red Hat es4. I know the default version of gcc for RHES4 is 3.4 which I need gcc34 in my fedora. ( I need to yum another package ‘compat-gcc-34.x86_64’ for that) . With that now I can compile the codes at my x86_64 fedora to produce binary that can run at RHES4.

gcc34 -m32 -o hello{,.c} -lxml2

3 Responses to “Compile and execute 32 bits application in 64 bits operating system”

  1. I am on Fedora 12 on x86_64 arch. I have installed glibc.i686 package using yum. When I compile a simple program,I get the following:

    [dks@Local2 ~]$ gcc -m32 junk.o

    /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.4.2/ when searching for -lgcc_s
    /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.4.2/ when searching for -lgcc_s
    /usr/bin/ld: cannot find -lgcc_s
    collect2: ld returned 1 exit status

    What more do I need to install. Thanks.

  2. 145rareweensongs145joints wordpress com 2014 12 10 89 little miss mandee aka whiskey frisky prick

  3. I use as/ld to create execs from at&t gas assembly files, for instance if I have an assembly source file ‘a_file.s’, to create an executable ‘a_file’

    $> as –32 -o a_file.o a_file.s

    this creates an object file ‘a_file.o’ (specified by the ‘-o’ option) for 32bit (specified by the ‘–32’ option) from the source, a_file.s

    An exec file is then created from the ‘a_file.o’ object file (created by as) using the linker ld…

    $> ld -m elf_i386 -o a_file a_file.o

    the -m option to ld says to emulate elf_i386, -o says to create the exec file ‘a_file’ from the object file ‘a_file.o

    You can use the command:

    $> ld -V

    to list emulations supported on your linux

Leave a Reply