GCC - Compile 32-bit code on 64-bit system

16,574

The gcc-multilib package only provides 32-bit support for the current default compiler.

Since you are using a non-default compiler (gcc-4.8 versus the system's default gcc-6) you will need to install the underlying version-specific multilib package explicitly e.g.

sudo apt install gcc-4.8-multilib

(or the equivalent from your favorite package manager).

Share:
16,574

Related videos on Youtube

ruby-miner
Author by

ruby-miner

Updated on September 18, 2022

Comments

  • ruby-miner
    ruby-miner over 1 year

    I'm studying Assembly and I need to compile a piece of C code into a 32-bit executable file. The issue is that I keep getting this error. I've tried installing gcc-multilib and g++-lib, but it hasn't helped. Anyone know what might be going on?

    > gcc main.s -m32 -o main
    
    /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc.a when searching for -lgcc
    /usr/bin/ld: cannot find -lgcc
    /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc_s.so when searching for -lgcc_s
    /usr/bin/ld: cannot find -lgcc_s
    collect2: error: ld returned 1 exit status
    

    Using gcc version 4.8.5 (Ubuntu 4.8.5-4ubuntu4) on Ubuntu 16.10.

    Update: Added the compilation command. I compiled the .s file beforehand using gcc -m32 -S main.c because I needed to have a look at the Assembly code.

    • steeldriver
      steeldriver about 7 years
      Please edit your question to include the exact command you are using to compile the code - not just the error message
    • steeldriver
      steeldriver about 7 years
      Well I suspect that gcc-multilib only installs the 32-bit components for the default gcc (i.e. gcc-6 on 16.10) - have you tried installing the corresponding gcc-4.8-multilib?
    • Ioannis
      Ioannis almost 3 years
    • Ioannis
      Ioannis almost 3 years
  • r0ng
    r0ng over 3 years
    Hello, I am trying to use gcc-9.3.0 to build 32-bit code. But sudo apt install gcc-9-multilib g++-9-multilib does not work, meaning the compiler still tries to build 64-bit code. Any suggestions? Thanks.
  • steeldriver
    steeldriver over 3 years
    @r0ng can you be more specific than "doesn't work"? What happens, exactly? What is your Ubuntu version?
  • r0ng
    r0ng over 3 years
    I found the problem I added -I/usr/include/x86_64-linux-gnu/c++/9 in the make file, but forgot to remove it after I installed gcc-9-multilib.