Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a linux compiled program not work on Windows

Tags:

c

linux

windows

I am almost sure my problem is due to the fact that the compiled program is compiled as a linux executable, but I just want to double check this.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("Hello world!\n");
    return EXIT_SUCCESS;
}

The above "program" should compile on Windows and Linux just fine, since it is source code compatible, as there are no operating system specific libraries or anything like that.

Yet, when I type in "c99 hello.c -o hello.exe" on my Linux box, and then transfer that "executable" to a windows machine, it refuses to run. From what I understand, Linux generates an executable file that only runs on linux, so adding ".exe" has no effect. To build that program on Linux for Windows, I would need to recompile that program on a Windows machine? Or is there another simpler method that will work?

like image 552
Thomas E Avatar asked Aug 20 '15 11:08

Thomas E


People also ask

Can you compile Windows programs on Linux?

mingw32 exists as a package for Linux. You can cross-compile and -link Windows applications with it. There's a tutorial here at the Code::Blocks forum. Mind that the command changes to x86_64-w64-mingw32-gcc-win32 , for example.

Can compiled C code run on any computer?

A C program using nothing but standard C will compile (and THEN run) on any platform as long as you have a C compiler for it available. Show activity on this post. There is no such thing as a "C executable". A C compiler translates C source code to the specific target system.

Can you compile EXE on Linux?

You can only run Linux executables inside it and default gcc creates Linux executables. Note that these Windows executables will not work inside Linux Subsystem, only outside of it. Show activity on this post. To compile a program for windows inside linux you can use mingw.


Video Answer


1 Answers

Windows and Linux executable files use two different incompatible formats:

On Windows, it is the Portable Executable format.

On Linux it is the ELF (Executable and Linkable Format).

Each format makes uses of the specificities of the OS they are supposed to run on, so they can't normally be executed on another platform.

Also, an executable only contains a small portion of the code that is executed after it is loaded into memory. An executable file is linked to system libraries that provide most of the functionality that allows the program to interact with the system.
As systems are very different from each other, libraries vary, and a program for say, Linux, cannot be run on FreeBSD despite the latter using also ELF, because they are not linked to the same libraries.

To compile a Windows executable on Linux, a technique known as cross-compilation can be used. A compiler is after all just a program that writes into a binary file, so any compiler can in theory write code for any platform, as long as the target system's libraries are available to be linked against.

The MinGW-w64 project provides a toolchain that allows this. For example, you can install it on debian-based systems using the sudo apt-get install mingw-w64 command.

The installed executables can be used like this:

i686-w64-mingw32-gcc hello.c -o hello32.exe      # 32-bit
x86_64-w64-mingw32-gcc hello.c -o hello64.exe    # 64-bit
like image 171
SirDarius Avatar answered Oct 14 '22 13:10

SirDarius