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 Assemble
Even though CPU is the same, there are still many differences:
long
is 8 byte on Linux, but 4 bytes on Windows. (Type sizes and required alignments are another part of what makes an ABI, along with struct/class layout rules.)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.Programs interact with OS in a different way: on Linux program might do system call directly as those are documented and are a part of provided interface, while on Windows they are not documented and programs should instead use provided functions.
Even if a Linux program only calls the C library's wrapper functions, a Windows C library wouldn't have POSIX functions like read()
, ioctl()
, and mmap
. Conversely, a Windows program might call VirtualAlloc
which isn't available on Linux. (But programs that use OS-specific system calls, not just ISO C/C++ functions, aren't portable even at a source level; they need #ifdef
to use Windows system calls only on Windows.)
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 them. This is 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 efficient. Such program also have problems interacting with native programs as different non-interoperable interfaces are used.
Source-compatibility layers like Cygwin can be inefficient, too, when emulating POSIX system calls like fork()
on top of the Windows model. But in general Cygwin has an easier job than WINE: programs need to be recompiled under Cygwin. It doesn't try to run native Linux binaries under Windows.