I'm a new C language learner and for facility. I downloaded Ubuntu 20.04 LTS WSL on my PC for compiling the code in gcc
. The problem is, that the code compiled in Ubuntu runs well, but when I send it to the Windows it says that the program can't run because there is no version compatible with x64 bits Windows. I compiled it with the -m64 flag to ensure is x64 bits but it still does not run on the Windows. What is the reason for that?
-
2Did you cross compile for Windows ? – Marged Jul 29 '21 at 23:12
-
3Are you saying that you compiled the program in Linux (Ubuntu in WSL) and tried to run it Windows (outside of WSL)? That's no surprise; binaries are (typically? always?) OS-specific. – ikegami Jul 29 '21 at 23:59
-
@ikegami I would say always. Windows uses the Portable Executable (PE) Format, whereas Linux and Unix-like systems the Executable and Linkable Format (ELF). – Erdal Küçük Jul 30 '21 at 01:54
-
Each binary must be compiled for a specific platform: [Why do you need to recompile C/C++ for each OS?](https://stackoverflow.com/q/61644911/995714), [Why does a linux compiled program not work on Windows](https://stackoverflow.com/q/32117572/995714), [Is a Linux executable “compatible” with OS X?](https://stackoverflow.com/q/9439436/995714), [Why does my Linux-compiled binary not work when I run it on Windows?](https://stackoverflow.com/q/46400211/995714) – phuclv Jul 30 '21 at 03:06
-
@ErdalKüçük that's not true. For example mac uses Mach executable, and Linux can also run COFF or A.out format. There are also many executable formats on Windows lie NE, PE, PE+... – phuclv Jul 30 '21 at 15:25
2 Answers
Binaries (compiled programs) are meant to be used on a specific system: when you install gcc in Ubuntu it automatically defaults to the Linux version of gcc and this version is only capable of creating a Linux binary.
Just as Linux can't use .exe
files out of the box, Windows can't use Linux binaries out of the box, that's why you have that "there is no version compatible with x64 bits Windows", it's because the file you're trying to use is compatible with x64 Linux.
Knowing that, there's multiple ways to go forward from here:
- Install a windows compiler and use windows to both compile and run your code
- Continue using WSL2 both for compiling and running your code
- Install a Windows compiler in WSL, build from Ubuntu, copy the file to windows and execute it from there
- Install a Linux compiler on your windows machine, build it from there, copy it to WSL and execute it on Ubuntu
- Install a Linux machine and use it to compile and run your code natively
I'd probably go for 1 or 5, WSL2 is a nice tool but since you're limited to the terminal it's not really beginner friendly, I'd choose an OS, install a nice beginner friendly IDE on it (I started with Code::Blocks 10 years ago and I think it's still the most beginner friendly IDE there is) and start developing without the added complexity of WSL or even worse, cross-compiling.

- 726
- 5
- 20
I would suggest better compile your c program on the Windows system. Cross-compilation do work but you said you are new to this I would suggest install c compiler on the windows system and compile your code natively

- 366
- 5
- 8