How To Compile a 32-bit Executable on 64-bit Fedora 19

You know how sometimes you just have to compile and run a 32 bit program on a 64 bit machine, and it’s really annoying that it doesn’t seem to work?

Yeah, me too.

Turns out it’s actually pretty straightforward on Fedora 19.

First of all, you need to use the -m32 build flag. Below I’m using it to compile my program ‘pointers’:

gcc -m32 pointers.c -o pointers

You may run into the following error:

/usr/include/gnu/stubs.h:7:27: fatal error: gnu/stubs-32.h: No such file or directory
# include <gnu/stubs-32.h>
^
compilation terminated.

To fix this you need to install the 32 bit glibc-devel package. You can do this by running:

yum install glibc-devel.i686

Next, you might see this error:

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

Which means you need to install the 32 bit standard C++ library package. You can do this by running:

yum install libstdc++-devel.i686

Then you can quite happily compile your executable with the -m32 flag, as above:

gcc -m32 pointers.c -o pointers

And, as if by magic, you get a 32 bit exe that runs on your 64 bit machine.

You can use the file command to admire what you’ve made:

[faye@localhost src]$ file pointers
pointers: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), 
for GNU/Linux 2.6.32, BuildID[sha1]=0x39958fed2f5c099ad97fd7512e780066acfb1231, not stripped

9 Comments

  1. VJ
    Posted 1 February 2015 at 20:57 | Permalink

    Hi,

    When doing yum install libstdc++-devel.i686 I get below error.

    No package libstdc++-devel.i386 available

    Could you help on how to get this package?

  2. Posted 2 February 2015 at 09:26 | Permalink

    libstdc++-devel.i386 is part of the standard fedora repository. What OS and version are you running?

  3. Tony
    Posted 20 March 2015 at 02:51 | Permalink

    Try doing:
    1) yum install libstdc++-*.i686
    2) yum install glibc-*.i686
    3) yum install libgcc-*.i686

  4. manoranjan
    Posted 16 April 2015 at 11:34 | Permalink

    Hi,
    I want to compile a 32 bit c program in 64 bit machince .It get compiled by

    gcc -lncurses RdAcq_hanle_040814.c -o root

    but when executing it provide inappropriate results.May be due to bit difference.

    when I tried to run the following command
    gcc -m32 -lncurses RdAcq_hanle_040814.c -o root
    it gave me error
    “/usr/bin/ld: skipping incompatible /usr/lib64/libtinfo.so when searching for -ltinfo
    /usr/bin/ld: cannot find -ltinfo
    collect2: ld returned 1 exit status”
    Please help

  5. manoranjan
    Posted 16 April 2015 at 11:39 | Permalink

    Also to mention the OS is Fedora 16 64 bit.But the result I want is irespective of OS and dependant on 64 bit.
    Thanks in advance

  6. Posted 16 April 2015 at 18:00 | Permalink

    You need the 32 bit version of tinfo. I don’t know off the top of my head which package that comes in, but it shouldn’t be too hard to track down.

  7. Posted 16 April 2015 at 18:04 | Permalink

    Is libtinfo linked as part of ncurses? Whatever you are linking to, you need the 32 bit versions of.

  8. Posted 18 August 2015 at 10:23 | Permalink

    i am also suffering from same error when using lex.
    the output as follows:
    [root@localhost exam2015]# lex cw.l
    [root@localhost exam2015]# gcc lex.yy.c -ll
    /usr/bin/ld: cannot find -ll
    collect2: error: ld returned 1 exit status.

    please help me to solve this problem.

  9. akshay pandav
    Posted 18 August 2015 at 10:27 | Permalink

    hello ankush,
    you can try following some commands which will help you to resolve problem:

    [exam2015@localhost ~]$ su
    Password:
    [root@localhost exam2015]# sudo yum update lex..

    if not solved then you may re-install lex package.

    Hope this will help you….