Compiling C files on Ubuntu and using the executable on Windows

14

3

For some odd reason, make kept giving me tabulation errors when compiling on Windows, so I tried it on Ubuntu and it worked just fine.

Now I would like to use the executable I got from compiling on Ubuntu and use it on Windows. However, the file type I am getting is different when I download it on Windows, as it is just type file. Is it possible to run it on Windows given that it was compiled on Ubuntu?

hkj447

Posted 2019-06-03T13:23:52.373

Reputation: 251

8You cannot run Linux executables on Windows unless you are using a Windows Subsystem for Linux instance to run them. If you want an executable that can run within Windows you must compile it on Windows. – Ramhound – 2019-06-03T15:07:45.900

14make gave you errors on Windows possibly because it wasn't GNU make, or possibly it was confused by line endings. Of course, even if you could get make to work doesn't mean that the code will compile on Windows. – jamesdlin – 2019-06-04T03:23:09.907

3

An alternative (thus not an answer) might be to compile and run it in the Linux subsystem on Windows https://docs.microsoft.com/en-us/windows/wsl/install-win10.

– None – 2019-06-04T08:11:13.133

@Leonhard - correct! I don't have enough experience with it to write an answer but the WSL specifically allows Ubuntu Linux binaries to run on Windows 10 (subject to some restrictions, mainly: no GUI). It's intended to facilitate running a Ubuntu toolchain to build Ubuntu binaries on Windows (for deployment elsewhere, e.g., the cloud, or a VM, or a container). Depending on the OP's use case this may be the easiest approach of all. – davidbak – 2019-06-04T18:32:20.007

Answers

47

The standard compiler toolchain on Ubuntu will produce Linux executables, not Windows executables. It is possible to install a cross-compiler that will produce Windows executables - this Stack Overflow question and answers give some hints on how to install and run one.

john_e

Posted 2019-06-03T13:23:52.373

Reputation: 646

40This (cross compilation) is technically the right answer. Note however, that if OP is not familiar with compiling things in general (as it seems), it's probably better for them to try and get the software to compile on Windows, rather than set up cross-compilation, which is IMHO usually less well supported by tools. – sleske – 2019-06-04T06:58:17.447

You; straighten out the Makefile -even better, just import the code into an IDE, such as Eclipse of one of the MicroSoft ones, which will obviate the need for a hand -crafted IDE – Mawg says reinstate Monica – 2019-06-04T07:06:42.963

12

This is called cross-compiling. You need a "toolchain" (compiler, linker, etc.) that will generate the appropriate code and format, involving:

  • The target processor architecture. In your case is is probably the same (x86 or amd64), but sometimes you cross-compile for a different processor, for instance when you build an executable for an ARM processor on your PC.

  • The target ABI (those are the call conventions used).

  • The right format

  • The right libraries, including system libraries.

Depending on the project, this may be very easy (just a flag or an environment variable to set somewhere) or very difficult.

You'll find a few pointers here, here or here.

jcaron

Posted 2019-06-03T13:23:52.373

Reputation: 986

And all the proper header files for the target system. – Andrew Henle – 2019-06-04T13:07:08.333

1

Make build scripts that work for Linux don't normally work for Windows.

You can try compiling your .c file without the Make subsystem (use directly Visual Studio, or another C compiler)

You could use Cygwin to compile and run the application if that application relies on POSIX calls.

You could try find the Windows-specific build script for the application: this normally is in a different folder or a different download altogether.

pestatije

Posted 2019-06-03T13:23:52.373

Reputation: 19

"Make build script that work for Linux don't normally work for Windows" - this is opinion-based, and not factual. Since OP did not specify what they are trying to compile, making assumptions about whether or not it is written to be compiled on Windows is pointless. – MechMK1 – 2019-06-05T13:19:21.567

@MechMK1 Actually if you read the subject line, the OP is asking about Compiling C code. – OldTimer – 2019-06-29T02:21:51.190

@OldTimer Yes, but not which exact project – MechMK1 – 2019-06-29T11:21:39.757