Run 32-bit application on Ubuntu on Windows subsystem for Linux

I have a pretty simple application that was compiled with g++ -m32 flags.

Unfortunately, when I try to run it I am getting error:

bash: ./a.out: cannot execute binary file: Exec format error

If compiled without -m32 there is no error and it runs as expected.

What can be a problem and is it possible to solve it?

EDIT:

  1. Compiling with g++ -m32 --static doesn't solve the issue.
  2. Setup is Ubuntu sub-system on Windows 10.

Solution 1:

Update (WSL2)

WSL2 runs in a real virtual machine using a real Linux kernel, so it's actually possible to do anything a Linux VM can do, including running 32-bit code. Just install 32-bit libs by running

sudo dpkg --add-architecture i386
sudo apt-get update

But again, you should really opt for the 64-bit version, especially if you have the source code. 32-bit Linux is much less maintained than the 64-bit version, since there are far fewer 32-bit only hardware in the last decade along with a significantly less number of users and testers. One example is that the Meltdown/Spectre patch for 32-bit Linux came out more than half a year later than the 64-bit version, and it's still buggy for years later


Original Answer

You simply can't do that. WSL doesn't support 32-bit apps!

Most (if not all) Intel and AMD CPUs in the last decade support the x86_64 instruction set and there's no reason for Microsoft to spend time and money on supporting a 32-bit version of Linux. Most developers and Ubuntu users are running the 64-bit version anyway

No, we don’t support x86 32-bit at this time: We currently depend on x64-only instructions and mechanisms to ensure fast & stable performance.

Bash on Ubuntu on Windows

  • Support for 32-bit i386 ELF binaries
  • Please enable WSL to run 32 bit ELF binaries
  • Exec format error 32-bit executable Windows Subsystem for Linux?
  • Windows Subsystem for Linux Overview: We recently announced Bash on Ubuntu on Windows which enables native Linux ELF64 binaries to run on Windows via the Windows Subsystem for Linux (WSL)

Solution 2:

In addition to the answer for wsl 2, I also had to install sudo apt-get install gcc-multilib so libc6 have been installed and ELF 32bit can be executed