23

gcc -m32 main.c -o main当我尝试在Windows Subsystem for Linux上执行编译的 32 位文件时,我收到以下错误:bash: ./main: cannot execute binary file: Exec format error.

如果我在没有 -m32它运行的情况下编译它。

在 WSL 上运行 32 位可执行文件的任何解决方案?

4

3 回答 3

46

QEMU 和 binfmt 支持点亮方式:)

https://github.com/microsoft/wsl/issues/2468#issuecomment-374904520

在阅读了 WSL 和 Windows 进程之间的 WSLInterop 使用 binfmt 之后,我正在修补 QEMU 来尝试一些 ARM 开发,并偶然发现了如何让 32 位支持工作。

编辑:需要“秋季创作者更新”,1709,内部版本 16299 或更新版本

安装 qemu 和 binfmt 配置:

sudo apt install qemu-user-static
sudo update-binfmts --install i386 /usr/bin/qemu-i386-static --magic '\x7fELF\x01\x01\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x03\x00\x01\x00\x00\x00' --mask '\xff\xff\xff\xff\xff\xff\xff\xfc\xff\xff\xff\xff\xff\xff\xff\xff\xf8\xff\xff\xff\xff\xff\xff\xff'

每次启动 WSL 时都需要重新激活 binfmt 支持:

sudo service binfmt-support start

启用 i386 架构包:

sudo dpkg --add-architecture i386
sudo apt update
sudo apt install gcc:i386

试试看:

$ file /usr/bin/gcc-5
/usr/bin/gcc-5: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=2637bb7cb85f8f12b40f03cd015d404930c3c790, stripped

$ /usr/bin/gcc-5 --version
gcc-5 (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc helloworld.c -o helloworld

$ ./helloworld
Hello, world!

$ file helloworld
helloworld: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=3a0c7be5c6a8d45613e4ef2b7b3474df6224a5da, not stripped

为了证明它确实有效,请禁用 i386 支持并重试:

$ sudo service binfmt-support stop
 * Disabling additional executable binary formats binfmt-support [ OK ]

$ ./helloworld
-bash: ./helloworld: cannot execute binary file: Exec format error
于 2018-03-21T11:50:21.350 回答
13

WSL(还)不提供 32 位 ELF 支持。自从 UserVoice 提出以来似乎没有任何进展 - 你运气不好。

请参阅UserVoice:请向内核添加 32 位 ELF 支持支持 32 位 i386 ELF 二进制文件

如果可能,切换到真正的Linux ;-)


由于最初发布此内容,因此支持真正的 Linux 内核的 WSL2 上已提供支持!所以这应该是首选方式。

如链接的github 问题中所述,如果仍使用 WSL1,也qemu-user可以使用。

于 2017-02-08T18:42:40.693 回答
9

WSL2 在使用真实Linux 内核的真实虚拟机中运行,因此实际上可以执行 Linux VM 可以执行的任何操作,包括运行 32 位代码。只需通过运行安装 32 位库

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

欲了解更多信息,请阅读

于 2019-11-21T17:19:57.513 回答