Travier
Travier

Reputation: 225

The CPU dependency of C - is it really only CPU dependency?

C is a compiled language that compiles to native CPU instructions, as you know.

But if C compiles differently to each CPU, then why don't basic Windows programs work on a Mac, for example (considering they both have a processor from the Intel 386 family)?

A good example of that is the printf function in C, that would compile differently on Windows and on a Mac OS X, even if they both sit on the same kind of processor (Intel 386).

I'm not actually sure that it really compiles differently under each OS, but the fact that a simple Windows application that outputs "Hello World!" to the Console Window won't open on a Mac, kind of make it seem like it compiles slightly differently.

I know for sure that GUI compiles differently because it calls system functions to draw on the screen, but why printf compiles differently on each OS? I thought it was CPU dependent, not OS dependent, and from what I know a Windows PC and a Mac both having an Intel 386 CPU means they both have the same instruction set.

So can you guys explain this, please?

Upvotes: 0

Views: 534

Answers (1)

marko
marko

Reputation: 9169

The C standard specifies the language and a very API and expected behaviour of a very minimal run-time library to support it.

It specifically does not specify the specifics of object-file format produced by the compiler - necessarily so - as we'd expect object files produced by C compiler to link with other libraries provided by the platform, and for the system linker to link them.

Taking your example of two systems with CPUs running on hardware with the same architecture (Windows and MacOSX) - the way dynamic linking on these platforms works under the hood is quite different, as are the object files produced by the compiler. Even with systems using the same CPU, there may also be ABI differences as well.

Upvotes: 4

Related Questions