This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why an executable program for a specific CPU does not work on Linux and Windows?
Why can't programs written in linux be executd in windows ? Suppose I compile a simple C program containing function calls that are common to both windows and linux, Does the compiler generate different binary under windows and linux ?
They use different container formats.
Most Linux executables are ELF files; all Windows executables and DLLs are PE files.
Here are some of the reasons I can think of off the top of my head:
- Different container formats (which so far seems to be the leading differentiator in this answer -- however its not the only reason).
- different dynamic linker semantics.
- different ABI.
- different exception handling mechanisms -- windows has SEH -- upon which C++ exception handling is built
- different system call semantics and different system calls -- hence different low-level libraries.
The binary types are different. For example, Linux may use the Executable and Linkable format, while Windows uses Portable Executable format.
But the biggest problems are the API's. A Windows program would call a Windows API to set up it's process, like stack, and allocate memory. Obivously those API calls are not available on other operating systems.
Yes, the executables use different file formats. In both cases, loading an executable to create a process involves a substantial amount of work, and neither (at least directly) includes the code to deal with loading the others binary format. Even if it did, most programs would have substantial problems. Just for example, quite a few Linux programs link against a shared library, so to load them successfully under Windows you'd not only need the loader, but also a copy of something to stand in place of that shared library. In reality, of course, there isn't just one shared library though -- there are dozens. By the time you emulated them all, you'd have a fairly substantial chunk of the OS as a whole ported to Windows.
There is no single function call you can make in both windows and linux that can affect anything out of the process' address space, even if you could get both systems to execute the program. Except maybe:
void f()
{
*((char*)0) = 0;
}