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.

  • 4
    without ever doing this id think you would have to use a cross compiler – Hayden Thring Sep 25 at 0:44
  • 32
    Since it's a C++ program, why don't you consider compiling and delivering the executable on a windows machine separately? – Raju Devidas Sep 25 at 0:45
  • 8
    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 Sep 25 at 7:43
  • 3
    Please do not cross post: stackoverflow.com/questions/52490846/… – Greenonline Sep 26 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 Sep 26 at 19:22

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.

  • 37
    Depending on how you look at it, running under WSL is "native" under Windows, though not pre-installed by default. – Bob Sep 25 at 8:14
  • 13
    @Bob In the same sense that running a Windows program under WINE is “native”. – Socob Sep 25 at 12:21
  • 17
    @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 Sep 25 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). – Ismael Miguel Sep 26 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 Sep 26 at 7:47

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
    The SuSE version works quite well too. – cup Sep 26 at 9:44

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.

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.

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

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.

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

  • 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. – Axel Kemper Sep 25 at 9:38
  • 9
    There won't be an equivalent MSI or EXE because the OP is compiling the code themselves. – selectstriker2 Sep 25 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. – Aaron Sep 25 at 22:18
  • 2
    The linked source from quora seems low quality and difficult to understand. – Aaron Sep 25 at 22:20
  • 4
    @Aaron - It's also not quoted, so this answer, is not much more than a link only answer. – Ramhound Sep 25 at 23:41

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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