1
votes

If the compiler coverts source code to machine code (binary) for a specific processor (let say intel), why do we need a compiler for linux and a different compiler for windows is both operating systems have the same processors ? Why is the compiler platform dependent ?

Why can't i run the binary compiled file (let's say i've compiled the file in linux with gcc -Wall -o file file.c) both on linux and widows without recompiling ?

Thanks

1

1 Answers

1
votes

Imagine you have your executable. The operating system has to load it in memory. Even this is not portable. What to load, where the code is, which functions it will import from other DLLs and so on is written inside the executable file and each OS has its own format. See these links:

Now imagine that because of magic the OS has everything in memory, mapped with its structures and it even knows the address of the main function to call. Your program, even to write a string on the screen has to call a CRT function or an OS API. With CRT it doesn't know what to do, they are different in the two environments and even API are different.

Even if, imagine again (Windows implemented a subset of POSIX API), it has a common function to call he doesn't know how to call it because of different calling conventions.