what are the differences between an executable gen

2020-02-29 10:47发布

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 ?

5条回答
We Are One
2楼-- · 2020-02-29 11:25

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.

查看更多
不美不萌又怎样
3楼-- · 2020-02-29 11:26

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;
}
查看更多
老娘就宠你
4楼-- · 2020-02-29 11:27

They use different container formats.

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

查看更多
forever°为你锁心
5楼-- · 2020-02-29 11:27

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.

查看更多
贼婆χ
6楼-- · 2020-02-29 11:35

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.
查看更多
登录 后发表回答