Trying (and failing) to run Hello World a 32-bit C ++ program on 64-bit Ubuntu on Windows 10

I configured my new Ubuntu on Windows 10 from scratch as follows:

# apt-get update # apt-get install build-essential # # Am able to compile now using "g++ -Wall -o Hello-World Hello-World.cpp", the binary is working. # # To check versions, and that both packages were indeed installed # gcc -v # make -v # apt-get install g++-multilib # # This also installs gcc-multilib as a dependency # # Now able to compile using "g++ -m32 -Wall -o Hello-World Hello-World.cpp # # However the binary Hello-World can't be run. Error message "bash: ./Hello-World: cannot execute binary file: Exec format error # apt-get install lib32gcc1 lib32stdc++6 # # Those two packages are at this time already both installed and well # dpkg --add-architecture i386 # apt-get update # apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 # # Still getting the same error when wanting to ./Hello-World 

I assume that I am still missing xyz:i386 library , I just could not figure out which one is still missing. Also, I'm not sure if this is a "Ubuntu on Windows" issue, or if this also happened when upgrading to a regular 64-bit Ubuntu OS in a similar way. Do you have any suggestions?

And to complete, this is the contents of the Hello-World.cpp :

 #include <iostream> using namespace std; int main (int argc, char **argv) { cout << "Hellobaby" << endl; return 0; } 
+5
source share
3 answers

It seems to me that Sam Warsawczyk was right, and Ubuntu on Windows does not currently support 32-bit architecture programs. I installed the virtual 64-bit Ubuntu on VirtualBox, and there - using the same commands that are described in my initial entry - compiling and running the program.

Thank you all for your comments.

+1
source

I think you have not installed all the g ++ related dependencies. Run the following commands to install the dependencies.

 sudo apt-get install g++ 

enter image description here

enter image description here

+1
source

QEMU and binfmt support make it easy :)

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

After reading that WSLInterop used binfmt between WSL and Windows processes, I worked on QEMU to try some ARM designs and accidentally discovered how to work with 32-bit support.

Requires an update to Fall Autodesk, 1709, build 16299 or later

Install qemu and binfmt configuration:

 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' 

You will need to reactivate binfmt support every time you start WSL:

 sudo service binfmt-support start 

Enable i386 architecture packages:

 sudo dpkg --add-architecture i386 sudo apt update sudo apt install g++:i386 

Try:

 $ file /usr/bin/g++-5 /usr/bin/g++-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]=9835ed3e5b1c8707591630e314ba4030a571deec, stripped $ /usr/bin/g++-5 --version g++-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. $ g++ -m32 -Wall helloworld.cpp -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]=65905fae78b837162a29d618b4ce63d300c62cb6, not stripped 

And to make sure it really works, disable i386 support and try again:

 $ sudo service binfmt-support stop * Disabling additional executable binary formats binfmt-support [ OK ] $ ./helloworld -bash: ./helloworld: cannot execute binary file: Exec format error 
0
source

Source: https://habr.com/ru/post/1247266/


All Articles