tags:

views:

168

answers:

5

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 ?

+3  A: 

They use different container formats.

Most Linux executables are ELF files; all Windows executables and DLLs are PE files.

SLaks
Back in the day, Windows supported `COM` executables which was basically just the raw assembly - no header whatsoever. Anyone know if it still does so?
Anon.
What does Mac Os support?
Hamish Grubijan
@lpthnc: Mac OS X uses the Mach-O format, inherited from NeXTSTEP. Classic Mac OS used the PEF format.
ephemient
Thank you.........
Hamish Grubijan
+2  A: 

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.

Andomar
That's why he wrote, `Suppose I compile a simple C program containing function calls that are common to both windows and linux`
SLaks
Well, even if you don't call any functions, the compiler has to generate API calls to set up a stack
Andomar
wine and cygwin FTW
Hassan Syed
@andomar the compiler doesn't make the calls the process does....
Hassan Syed
The code can be built to compile on both platforms with compiler instructions (ie. to include the Windows API and call WinMain) if you are compiling for that platform.
Tim
It doesn't matter what function calls he writes into his program. The startup sequence (the stuff that runs before his `main()` ever starts) is likely to be quite machine and OS dependent, and quite different between Windows and Linux.
Carl Smotricz
@Hassan: Andomar never said it did. However, the compiler still has to *generate* those API calls.
Anon.
API calls to set up a stack? Is that like creating a GUI in Visual Basic to track IPs? The stack is set by the time the entry point is reached on both systems. The completely different ABI and executable formats are far more relevant.
jbcreix
+3  A: 

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.

Jerry Coffin
A: 

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;
}
Richard Pennington
+2  A: 

Here are some of the reasons I can think of off the top of my head:

  1. Different container formats (which so far seems to be the leading differentiator in this answer -- however its not the only reason).
  2. different dynamic linker semantics.
  3. different ABI.
  4. different exception handling mechanisms -- windows has SEH -- upon which C++ exception handling is built
  5. different system call semantics and different system calls -- hence different low-level libraries.
Hassan Syed