Why do we need to compile for different platforms

2019-04-12 05:44发布

I've learned the basics about CPUs/ASM/C and don't understand why we need to compile C code differently for different OS targets. What the compiler does is create Assembler code that then gets assembled to binary machine code. The ASM code of course is different per CPU architecture (e.g. ARM) as the instruction set architecture is different.

But as Linux and Windows run on the same CPU, the machine operations like MOVE/ADD/... should be identical. While I do know that there are OS-specific functions like printing to a terminal, this functionality could be provided by different implementations of stdio.h, for example. And still, I could create a very basic program that just calculates a + b without printing anything, so that I do not need any OS-specific code. Why do I still need to compile for Linux and for Windows instead of just adding an .exe-Extension to my Linux executable?

4条回答
2楼-- · 2019-04-12 06:26

This is like saying if I use the same alphabet all books are the same a Biology text book and a Math text book are identical because they use the same alphabet have a cover have some pages, etc. Or I have to ski resorts and because they both use the same alphabet and because they both are about snow that their posters and brochures are identical.

int main ( void )
{
    return(27);
}

0000000000402cd0 <main>:
  402cd0:   48 83 ec 28             sub    $0x28,%rsp
  402cd4:   e8 d7 e9 ff ff          callq  4016b0 <__main>
  402cd9:   b8 1b 00 00 00          mov    $0x1b,%eax
  402cde:   48 83 c4 28             add    $0x28,%rsp
  402ce2:   c3                      retq   

  00000000004003e0 <main>:
  4003e0:   b8 1b 00 00 00          mov    $0x1b,%eax
  4003e5:   c3                      retq   

subtle differences sure, but the key is that these are two completely different operating systems, the entry/exit of the program (there is a TON of code not shown above that varies, not just the wee bitty spec if main code in this program.

These are different operating systems, they have different calls different rules, they are different, the instruction set being common is somewhat irrelevant. Its like saying because I am running on linux and using C as my programming language then a binary made for arm and a binary made for x86 should be identical and compatible (because two of the three things I said were the same, programming language and operating system but not instruction set. or in your case programming language and instruction set but not operating system.)

This goes so far as to point out that a gcc compiled program for windows is not completely compatible across all versions of windows, you cant just say "windows". same goes for linux. they change within themselves independent of target, then there are incompatible differences between the operating systems. Just because the brick and mortar are the same doesnt make two identical buildings.

This is the purpose of JAVA and Python and such languages, to draw a line everything above this line is common and cross platform, what is below this line can be platform and target specific and no reason to expect any form of cross platform compatibility. Those languages wouldnt exist if we had this kind of compatibility across the world of computers with C compilers or computers all running linux independent of platform or all running an operating system with a compiler and the same instruction set.

There is a reason when you go download some program like chrome or 7-zip or firefox, handbrake, etc there are different installers and/or binaries based on the operating system, and operating system version. The instruction set is often not even listed as it is assumed to be x86, yet there are different binaries, if it were this trivial then why would those folks who have delivered finished products for so long be delivering several different builds of the product?

查看更多
迷人小祖宗
3楼-- · 2019-04-12 06:30

Even though CPU is the same, there are still many differences:

  • Different executable formats.
  • Different calling conventions might be used. For example Windows requires callee to save xmm4-xmm5 registers, while other OS do not require this.
  • Different conventions regarding stack structure. Unix-like system have a concept of "red zone" to help compiler generate shorter code. Execution environment has to honor such concept to avoid stack corruption.
  • Programs are linked against different standard libraries with different ABIs - field order might differ, additional extension fields might be present.
  • Standard libraries can provide different set of functions. On Linux libc provide functions like snprintf directly, but on Windows snprintf might be implemented as static inline function in a header file that actually calls another function from C runtime. This is transparent for programmer, but generates different import list for executable.
  • In both C and C++ some data types have OS dependent sizes. For example on x86_64 long is 8 byte on Linux, but 4 bytes on Windows.
  • Programs interact with OS in a different way: on Linux program might do system call directly as those are documented and a part of provided interface, while on Windows they are not documented and programs should instead use provided functions.
  • Not OS related, but programs compiled by different compilers might not be interoperable: different standard libraries might be used, things like C++ name mangling might be different, making it impossible to link libraries against each other, C++ exception implementation might be non-interoperable.
  • Different filesystem structure. Not only there is a difference between "\" on Windows and "/" on Unix-likes, but there are "special files" that might or might not be present like "/dev/null".

In theory everything listed here can be resolved: custom loaders can be written to support different executable formats, different conventions and interfaces do not cause problems if the whole program uses the same set of everything. This why projects like Wine can run Windows binaries on Linux. The problem is that Wine has to emulate functionality of Windows NT kernel on top of what other OSes provide, making implementation less effecient. Such program also have problems interacting with native programs as different non-interoperable interfaces are used.

查看更多
成全新的幸福
4楼-- · 2019-04-12 06:34

In addition to everything else even with identical instructions even the calling conventions can differ, that is the placement of parameters on the stack or in registers, the order parameters are found, what registers must be preserved across a function call, how return values are passed from callee to caller.

查看更多
疯言疯语
5楼-- · 2019-04-12 06:45

You might want to check the other answers.

Its kind of duplicate question except that its for C not C++

You can check process of compilation steps here:

C compilation steps

In short , even though C is can be cross platform to run, due to compilers its not cross-platform compile-able.

查看更多
登录 后发表回答