gpt4 book ai didi

gcc - Exec 格式错误 32 位可执行 Windows Linux 子系统?

转载 作者:行者123 更新时间:2023-12-01 17:56:27 25 4
gpt4 key购买 nike

当我尝试在 Linux 的 Windows 子系统 上执行使用 gcc -m32 main.c -o main 编译的 32 位文件时,出现以下错误: bash: ./main: 无法执行二进制文件: Exec 格式错误

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

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

最佳答案

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

关于gcc - Exec 格式错误 32 位可执行 Windows Linux 子系统?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42120938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com