简体   繁体   中英

C compiled program in linux run there but does not in windows

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?

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:

  1. Install a windows compiler and use windows to both compile and run your code
  2. Continue using WSL2 both for compiling and running your code
  3. Install a Windows compiler in WSL, build from Ubuntu, copy the file to windows and execute it from there
  4. Install a Linux compiler on your windows machine, build it from there, copy it to WSL and execute it on Ubuntu
  5. 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.

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

How to compile C code in Linux to run on Windows?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM