Why does a linux compiled program not work on Windows

Thomas E

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?

SirDarius

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does a CoreRT-compiled program fail to work with ZIP files?

From Dev

why does this program not work?

From Dev

C++: Linux code does not work on Windows - piping data to program

From Dev

why does this recursion program work?

From Dev

Why does this Haskell program perform strange when compiled with -threaded?

From Dev

Why does this Haskell program leak space when compiled with optimizations?

From Dev

Program hangs on Linux and Windows, Seems to work on Mac

From Dev

Why compiled code creates corrupted file in windows in linux works fine

From Dev

Mono Application Compiled Under Linux Does Not Run in Windows

From Dev

Mono Application Compiled Under Linux Does Not Run in Windows

From Dev

Why does my program not work as expected?

From Dev

Why does this Perl program not work as advertised

From Dev

Why does this Perl program not work as advertised

From Dev

ngRepeat in compiled by $compile does not work

From Dev

why doesnt my program work as it should? (why does it stop in the middle)

From Dev

Why does this movq instruction work on linux and not osx?

From Dev

why does sudo work on Linux but not Android?

From Dev

why does sudo work on Linux but not Android?

From Dev

Why does `^[ ]{0,}` not work with linux grep?

From Dev

Traceroute doesn't work on Linux, on Windows it does

From Dev

DNS does not work equally on Windows and Linux

From Dev

Why can't I see the icon that is compiled inside my Windows program as a binary Resource?

From Dev

Windows 8.1 why does RabbitMQ dont work?

From Dev

Understanding ASM. Why does this work in Windows?

From Dev

Why 'ipconfig' does not work on Windows 7

From Dev

Windows 8.1 why does RabbitMQ dont work?

From Dev

Why does `ls` work on my cmd? (Windows)

From Dev

Why does chmod not work on bash emulation on windows?

From Dev

Why does this multi-threaded program work (and not crash)?

Related Related

  1. 1

    Why does a CoreRT-compiled program fail to work with ZIP files?

  2. 2

    why does this program not work?

  3. 3

    C++: Linux code does not work on Windows - piping data to program

  4. 4

    why does this recursion program work?

  5. 5

    Why does this Haskell program perform strange when compiled with -threaded?

  6. 6

    Why does this Haskell program leak space when compiled with optimizations?

  7. 7

    Program hangs on Linux and Windows, Seems to work on Mac

  8. 8

    Why compiled code creates corrupted file in windows in linux works fine

  9. 9

    Mono Application Compiled Under Linux Does Not Run in Windows

  10. 10

    Mono Application Compiled Under Linux Does Not Run in Windows

  11. 11

    Why does my program not work as expected?

  12. 12

    Why does this Perl program not work as advertised

  13. 13

    Why does this Perl program not work as advertised

  14. 14

    ngRepeat in compiled by $compile does not work

  15. 15

    why doesnt my program work as it should? (why does it stop in the middle)

  16. 16

    Why does this movq instruction work on linux and not osx?

  17. 17

    why does sudo work on Linux but not Android?

  18. 18

    why does sudo work on Linux but not Android?

  19. 19

    Why does `^[ ]{0,}` not work with linux grep?

  20. 20

    Traceroute doesn't work on Linux, on Windows it does

  21. 21

    DNS does not work equally on Windows and Linux

  22. 22

    Why can't I see the icon that is compiled inside my Windows program as a binary Resource?

  23. 23

    Windows 8.1 why does RabbitMQ dont work?

  24. 24

    Understanding ASM. Why does this work in Windows?

  25. 25

    Why 'ipconfig' does not work on Windows 7

  26. 26

    Windows 8.1 why does RabbitMQ dont work?

  27. 27

    Why does `ls` work on my cmd? (Windows)

  28. 28

    Why does chmod not work on bash emulation on windows?

  29. 29

    Why does this multi-threaded program work (and not crash)?

HotTag

Archive