本人在做gcc编译实验时需要将c程序编译为32位程序并运行,运行环境是win10子系统-Ubuntu 18.04 LTS,编译欧克,运行总会出现以下错误:
root@DESKTOP-ROQVS0I:~/STL$ ./test1
-bash: ./test1: cannot execute binary file: Exec format error
我的编译指令是:
gcc -O0 -m32 -g test.c -o test
后来经过网友提供的信息得知,win10子系统只支持64位程序的编译运行,可以安装一些库,完美运行32位程序
sudo dpkg --add-architecture i386
sudo apt update
sudo apt install -y libc6:i386 libncurses5:i386 libstdc++6:i386 zlib1g:i386 zlib1g-dev:i386
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'
sudo service binfmt-support start
经过以上一顿操作,运行:
root@DESKTOP-ROQVS0I:~/STL$ ./test1
hello world!
本文参考知乎用户“封尘印”的回答
原文地址:https://github.com/Microsoft/WSL/issues/2468