7

I compiled a C++ program under Linux:

make -f mymakefile

This will generate an executable, a.out. It seems this executable cannot be run in the Windows command prompt.

7
  • 4
    without ever doing this id think you would have to use a cross compiler Commented Sep 25, 2018 at 0:44
  • 33
    Since it's a C++ program, why don't you consider compiling and delivering the executable on a windows machine separately?
    – rajudev
    Commented Sep 25, 2018 at 0:45
  • 9
    Can you explain what the C++ program does? If it's a command line program, then it would probably work if recompiled under the correct compiler on Windows (I presume GCC, but depends on the code). If it uses a GUI, then maybe it could compile on Windows, but maybe not. If it's some sort of daemon or device driver, then almost definitely, it won't compile on Windows.
    – Neil
    Commented Sep 25, 2018 at 7:43
  • 3
    Please do not cross post: stackoverflow.com/questions/52490846/… Commented Sep 26, 2018 at 14:22
  • 2
    @Greenonline Both that question and this one may have arisen from the same fundamental misunderstanding of the problems involved in sharing binary code across operating systems, but the actual questions being asked are very different. This is not a case of cross posting.
    – jmbpiano
    Commented Sep 26, 2018 at 19:22

7 Answers 7

94

You cannot natively run a program for Linux under Windows. They are completely different operating systems.

However, there are methods you can try to run the program:

  1. Recompile the program on Windows to get a native executable
  2. Install the Windows Subsystem for Linux and run the program in that environment
  3. Install Linux in a virtual machine and run the program in that environment
  4. Install Cygwin or MinGW and recompile and run in that environment
  5. Use a cross compiler

Granted, depending on the nature of the program and its dependencies, it might not be possible to run in another environment without additional software, modifications to the source code, or at all.

6
  • 40
    Depending on how you look at it, running under WSL is "native" under Windows, though not pre-installed by default.
    – Bob
    Commented Sep 25, 2018 at 8:14
  • 14
    @Bob In the same sense that running a Windows program under WINE is “native”.
    – Socob
    Commented Sep 25, 2018 at 12:21
  • 19
    @Socob Not quite: WSL is part of the NT kernel. But otherwise, yes Wine is as close as you'll get to running a Win32 binary natively on Linux.
    – Bob
    Commented Sep 25, 2018 at 13:17
  • 1
    Using a VM with Debian (without desktop) + Putty + xming is actually a really cool setup to test Linux executables. Also, it has the added benefict of adding a little bit of safety, since it runs in a separate system. If you install a malicious program or, in the worst case, a mirror gets hacked and distributes infected updates, it will only affect one machine in a different subnet (by default). Commented Sep 26, 2018 at 1:49
  • 7
    @Bob the reason WINE can run in userspace and WSL must have kernel components is that on Linux syscalls are part of the ABI, whereas on Windows everything is virtualized via calls to functions in ntdll.dll
    – kinokijuf
    Commented Sep 26, 2018 at 7:47
26

You need the Linux subsystem for Windows (WSL) and a Linux distribution. The Windows store has a few Linux distributions prepackaged with WSL. Ubuntu is fairly popular, but since you already have a Linux system on which you built a.out, it might be easiest to match that.

If you can't match the Linux distributions, and a.out doesn't work as-built, it's also possible to re-run make on your WSL distribution

1
  • 1
    The SuSE version works quite well too.
    – cup
    Commented Sep 26, 2018 at 9:44
8

The answer above covered most of the aspects, but not sure if have come across flinux (sometimes called foreign linux) which happens to have been also suggested here and may be an easier workaround depending on what you are trying to achieve.

(Note I have WSL and work with emulators and VMs a lot, and I haven't really explorer other workarounds :))

Foreign LINUX is a dynamic binary translator and a Linux system call interface emulator for the Windows platform. It is capable of running unmodified Linux binaries on Windows without any drivers or modifications to the system. This provides another way of running Linux applications under Windows in constrast to Cygwin and other tools. It now runs a large bunch of console applications and some GUI applications.

1
  • the most ancient (1.0) or recent (2.1) flinux crash on start on Win10; but the intermediate version 2.0 runs perfectly well on my Win10 box. the logger flog runs smoothly in all cases. multumesc!
    – jarnosz
    Commented Sep 17, 2022 at 19:31
5

You can cross compile for Windows on Linux.

See https://stackoverflow.com/questions/2033997/how-to-compile-for-windows-on-linux-with-gcc-g

This allows you to use Linux to compile a binary executable program that runs under Windows.

3

Another option which is similar to running a Virtual machine, but not exactly the same is running your application from a Docker container.

Yes Docker for Windows uses a VM in the background (MobyLinuxVM on HyperV), but you can do something like this:

$ docker run a.out

and will stop the container on its own. It will also use less resources and the output can be read from Windows own terminals like cmd and PowerShell.

A dockerfile for this situation will look something like this:

FROM docker pull ubuntu:latest

RUN make -f mymakefile

I think personally this is the nicest solution for running Linux applications in Windows

0

For this particular case I myself used to install gcc on my windows 8 by mingw.

Then I would add path of my mingw folder to system path (from control panel/system/advanced system settings).

Then I could run gcc on my command prompt just like linux.

-7

Try to get the windows exe or msi equivalent of the linux executable and run or use cygwin to install linux executable.

There is a tool mobaxterm very helpful, have a look and you can get your task done. This tool has cygwin and other linux utility to proceed with.

Source: https://www.quora.com/How-do-I-run-Linux-executable-on-windows

5
  • 4
    MobaXterm appears to be a tool to access remote systems from a Windows system. It provides a terminal window (X server) and acts as ssh client. Therefore, it might help to develop and compile on a remote Unix system, but it has no cross compilation features. Commented Sep 25, 2018 at 9:38
  • 9
    There won't be an equivalent MSI or EXE because the OP is compiling the code themselves. Commented Sep 25, 2018 at 14:35
  • 2
    @AxelKemper I used MobaXterm years ago. If I recall, what you describe was a main attraction, but it actually did more than that. I do not recall exactly how much more, and I am not sure if it could be used as this answer states, but there was indeed more to it than is apparent on the surface.
    – Loduwijk
    Commented Sep 25, 2018 at 22:18
  • 2
    The linked source from quora seems low quality and difficult to understand.
    – Loduwijk
    Commented Sep 25, 2018 at 22:20
  • 4
    @Aaron - It's also not quoted, so this answer, is not much more than a link only answer.
    – Ramhound
    Commented Sep 25, 2018 at 23:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .