Compiling C files on Ubuntu and using the executable on Windows

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?


Solution 1:

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.

Solution 2:

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.

Solution 3:

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.