14

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?

  • 8
    You 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 Jun 3 '19 at 15:07
  • 14
    make 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 Jun 4 '19 at 3:23
  • 3
    An alternative (thus not an answer) might be to compile and run it in the Linux subsystem on Windows docs.microsoft.com/en-us/windows/wsl/install-win10. – user716816 Jun 4 '19 at 8:11
  • @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 Jun 4 '19 at 18:32
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.

| improve this answer | |
  • 40
    This (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 Jun 4 '19 at 6:58
  • 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 Jun 4 '19 at 7:06
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.

| improve this answer | |
  • And all the proper header files for the target system. – Andrew Henle Jun 4 '19 at 13:07
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.

| improve this answer | |
  • "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 Jun 5 '19 at 13:19
  • @MechMK1 Actually if you read the subject line, the OP is asking about Compiling C code. – OldTimer Jun 29 '19 at 2:21
  • @OldTimer Yes, but not which exact project – MechMK1 Jun 29 '19 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.