Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is C platform-dependent? [duplicate]

Tags:

c

Why does C have to be recompiled for each architecture? Doesn't it all end up as bits and bytes and loads and jumps anyway? Doesn't each architecture essentially go through the most basic commands in the same way?

like image 852
Aristides Avatar asked Aug 11 '13 11:08

Aristides


2 Answers

  1. Different CPUs have different instruction architectures (e.g., x86 vs ARM).
    • Early Macs used the Motorola 68k architecture; later ones used PowerPC; and still later ones used x86. During each of these transitions, developers had to ship their executables as fat binaries, which would contain object code for both architectures.
  2. Current x86 CPUs have 32-bit and 64-bit modes.
    • This is why you have 32-bit and 64-bit versions of Windows, Ubuntu, etc.
  3. Different operating systems provide different system calls, libraries, etc.
    • Different OS versions can provide different system calls, libraries, etc. also (although OS vendors do aim to be backward compatible as much as possible).
  4. Even on the same operating system, the calling convention is not guaranteed to be the same between different compilers.
    • Even on the same OS, different executable file formats may be in use. For example, on many Unix systems, a.out used to be the format used, but most eventually switched to ELF. During the transition period, libraries had to be provided in both formats.
like image 187
Chris Jester-Young Avatar answered Oct 07 '22 19:10

Chris Jester-Young


Doesn't each architecture essentially go through the most basic commands in the same way?

For the most part. But those basic commands are represented or implemented differently. The C compiler is responsible for making sure that the correct representations and implementations are used.

like image 20
Ignacio Vazquez-Abrams Avatar answered Oct 07 '22 18:10

Ignacio Vazquez-Abrams