8

I'm compiling C++ on Debian Linux (64 bit) and need to be able to compile to 32 bit for another system. The binaries won't run on my 64 bit system and having them run there would be far more convenient for testing.

My C++ for testing this is int main () { std::cout << "This is Main.cpp" << std::endl; } with iostream included, so nothing fancy there.

My compile line is g++ -m32 Main.cpp

When I do ./a.out I get -bash: ./a.out: cannot execute binary file: Exec format error

I have done quite a bit of searching trying to resolve this and have apt installed: libc6:i386, libncurses5:i386, and libstdc++6:i386.

Any other ideas to fix this would be much appreciated.

G3yost
  • 89
  • 1
  • 1
  • 3
  • What does `file ./a.out` tell you? Are you able to run 64-bit binaries (compile without `-m32`)? – Hadi Brais Apr 07 '18 at 23:24
  • @HadiBrais I am able to run 64-bit binaries without the `-m32` and when I compile with the `-m32` to get a 32-bit a.out and I do `file a.out` I get `a.out: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=e1483a60a1b26d9b3738bb963e90e713b515031b, not stripped` – G3yost Apr 07 '18 at 23:49
  • It should say `ELF 32-bit LSB executable...` not `ELF 32-bit LSB shared object...` Why is the compiler emitting a shared object? It should emit an executable. A shared object cannot be run using `./a.out`. – Hadi Brais Apr 08 '18 at 00:18
  • @HadiBrais huh... I will look into that. I'm not sure why I didn't think to compare `file` on a `-m32` and `-m64` formerly thought executables. I can't wait to dive down that rabbit hole. Thanks! – G3yost Apr 08 '18 at 00:45
  • Glad I could help. Please try to keep me updated on the issue. – Hadi Brais Apr 08 '18 at 00:53

1 Answers1

13

You need to install 32bit libraries, e.g.

dpkg --add-architecture i386
apt-get update
apt-get install libc6-i386

You can find out which libraries are needed using the ldd command. You can use apt-file to find the packages for the libraries.

Xypron
  • 2,215
  • 1
  • 12
  • 24
  • 1
    If you install libc6-i386, you shouldn't need add-architecture. If you do add the i386 architecture, you might as well install libc6:i386. No? – Marc Glisse Apr 07 '18 at 08:44
  • 1
    I do all that you have above, and when I do `apt install libc6-i386` apt says that it's already the newest version. Any suggestions from here? Thanks for your help. – G3yost Apr 07 '18 at 17:53
  • @MarcGlisse FWIW, I came here with the same problem. I had already tried installing libc6-i386, and it hadn't helped: I wasn't able to run 32-bit binaries; I was still missing /lib/ld-linux.so. The add-architecture step was what made it work for me, and I am utterly grateful to Xypron for the suggestion. – Steve Summit Mar 10 '21 at 15:55