Can not compile a 32 bit binary on a x86_64 host due to ld failure

Latest response

Not an unexpected message but not clear how to solve :

$ gcc -m32 -std=iso9899:1999 -fno-builtin -o hello hello.c
/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.8.5/libgcc_s.so when searching for -lgcc_s
/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status

So looks like I need the 32-bit libs for gcc but it isn't clear what their yum names are.

Strangely a symlink exists but no actual lib :

$ ls -lad /usr/lib/gcc/x86_64-redhat-linux/4.8.2/32/libgcc_s.so
lrwxrwxrwx. 1 root root 18 Dec 5 18:36 /usr/lib/gcc/x86_64-redhat-linux/4.8.2/32/libgcc_s.so -> /lib/libgcc_s.so.1
$ ls -lad /lib
lrwxrwxrwx. 1 root root 7 Dec 5 09:21 /lib -> usr/lib
$ ls -lad /usr/lib/libgcc*
ls: cannot access /usr/lib/libgcc*: No such file or directory
$

So that is odd.

Responses

Try installing libgcc.i686 which will provide /lib/libgcc_s-4.8.5-20150702.so.1.

Well that gives me the usual Red Hat baffle-gab :

yum install libgcc.i686

Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager Resolving Dependencies --> Running transaction check ---> Package libgcc.i686 0:4.8.5-28.el7 will be installed --> Finished Dependency Resolution Error: Multilib version problems found. This often means that the root cause is something else and multilib version checking is just pointing out that there is a problem. Eg.:

     1. You have an upgrade for libgcc which is missing some
        dependency that another package requires. Yum is trying to
        solve this by installing an older version of libgcc of the
        different architecture. If you exclude the bad architecture
        yum will tell you what the root cause is (which package
        requires what). You can try redoing the upgrade with
        --exclude libgcc.otherarch ... this should give you an error
        message showing the root cause of the problem.

     2. You have multiple architectures of libgcc installed, but
        yum can only see an upgrade for one of those architectures.
        If you don't want/need both architectures anymore then you
        can remove the one with the missing update and everything
        will work.

     3. You have duplicate versions of libgcc installed already.
        You can use "yum check" to get yum show these errors.

   ...you can also use --setopt=protected_multilib=false to remove
   this checking, however this is almost never the correct thing to
   do as something else is very likely to go wrong (often causing
   much more problems).

   Protected multilib versions: libgcc-4.8.5-28.el7.i686 != libgcc-4.8.5-16.el7_4.1.x86_64

yeah ... Red Hat ... gotta love it.

You need to update/install both x86_64 and i686 with the same version (4.8.5-28.el7 for 7.5). If you need to stay at 7.4, then install the i686 package that matches your existing libgcc-4.8.5-16.el7_4.1.

Yep, did all that. Now the symlink finally points to somewhere real. Strange that the symlink existed at all with no where to point to but I guess some package in the repo is put together badly.

GNU C (GCC) version 4.8.5 20150623 (Red Hat 4.8.5-28) (x86_64-redhat-linux)
        compiled by GNU C version 4.8.5 20150623 (Red Hat 4.8.5-28), GMP version 6.0.0, 
        MPFR version 3.1.1, MPC version 1.0.1

Strange how nearly everything in RHEL is so far out of date. Regardless, can now compile/link stuff to get a 32-bit ELF :

rhel74$ file hello
hello: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=2c0c660d03900c53f5643420ca6bf7c521aadc28, not stripped
rhel74$ readelf -delVt hello
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Intel 80386
  Version:                           0x1
  Entry point address:               0x8048460
  Start of program headers:          52 (bytes into file)
  Start of section headers:          6360 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         9
  Size of section headers:           40 (bytes)
  Number of section headers:         30
  Section header string table index: 27

... etc etc 
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.