user8768055
user8768055

Reputation:

Are compilers OS-specific?

I understand what a compiler does, in that it takes source code and compiles it to machine code that the computer can understand. However, I'm wondering one thing, which is that, is that machine code specific to every different operating system? Is it necessary to write a different compiler for the same language on Mac, Windows and Linux operating systems? Please elaborate if the answer is yes, no or in between.

Upvotes: 7

Views: 7877

Answers (3)

Glenn Teitelbaum
Glenn Teitelbaum

Reputation: 10333

keep in mind that compilers running on one OS can generate code for a different OS.

So if i write a compiler for OS A, and it can generate core for OS B, then I can compile that compiler written for A, but output for B. then i have a compiler for B. Technically some might not call it the same compiler.

Upvotes: -1

Mike Harris
Mike Harris

Reputation: 1576

It depends.

A compiler that generates machine instructions is machine (or architecture) specific: an ARM and an x86 have different instruction sets, so the machine code would be very different for each, but it's not necessarily OS-dependent (although it usually is, because different OSes have different calling conventions, etc.) The linker component is almost always OS-specific, since this is what creates the executable file for that OS. Not all compiler systems separate the compiler from the linker -- if it's one program, than it would be OS-specific.

However, there are OS/machine independent compilers: most notably Java (and other JVM-based languages, such as Scala or Groovy). This is because Java compilers generate bytecode for the JVM, and are executed by the Java runtime (which is OS and processor specific). But the Java compiler (which is written in Java) can run on any machine, and generate code that will run on any machine.

Upvotes: 10

Dougie
Dougie

Reputation: 485

The output from a compilation contains two things 1. Calls to operating system library routines 2. Native machine code to perform your "business logic"

The second part is, most likely, identical for every compilation that targets a specific processor architecture (why shouldn't it be if you're using the same version of GCC).

The calls to library routines are where things fall down. That's the reason WINE exists on Linux - it's to give you some "emulated" / "translated" routines that run on Linux to give you a way to run a Windows program on an alien operating system.

So the answer is really, not quite. Rather than an absolute yes or no.

Upvotes: 2

Related Questions