tags:

views:

317

answers:

4

Probably a pretty vague and broad question, but do all C++ compilers compile code into C first before compiling them into machine code?

+6  A: 

Nope. GCC for example goes from C++ -> assembler. You can see this by using the -S option with g++.

Actually, now that I think about it, I don't think any modern compiler goes to C before ASM.

Gianni
Comeau compiles C++ into C. (Which then goes on to be compiled into a binary.)
GMan
@GMan Wow! I've never heard of the Comeau, and you just gave me a very good reason to keep it that way!
Gianni
@Gianni: Actually Comeau is regarded as one of the most compliant compilers around. :) (That's all I know though; not a clue on how well it actually produces optimized binaries.)
GMan
@GMan OK, good point. But still, it isn't a terribly good reason, by itself...
Gianni
Though you are correct about g++ (not GCC) you cant tell that because of the -S option. Before it reaches assembly the code is actually translated through several intermediate formats (most of which can be dumped to file).
Martin York
In fact, Comeau's **C compiler** uses C as its intermediate represenation, too. Pretty much every compiler does a front-end translation into some intermediate representation - Comeau's intermediate representation happens to be C, which means he doesn't need to maintain a backend for each of the platforms he supports. Since he's a one-man shop (I think), that makes a certain amount of sense. I suppose he could have used whatever intermediate representation that GCC uses, but there might be licensing or other issues that made that less desirable.
Michael Burr
Actually, a compiler like that would be damn useful for the various small (eg: embedded) platforms around that only sport a C compiler. Unless you really *want* to use C I suppose. *shudder*
T.E.D.
+3  A: 

No. C++ -> C was used only in the earliest phases of C++'s development and evolution. Most C++ compilers today compile directly to assembler or machine code. Borland C++ compiles directly to machine code, for example.

dthorpe
+1  A: 

No. This is a myth, based around the fact that a very early version of Stroustrup's work was implmented that way. C++ compilers generate machine code in almost exactly the same way that C compilers do.

Today, the only C++ compiler that I am aware of that creates C code is Comeau. There may be one or two more for embedded targets, but it is certianly not a mainstream thing.

T.E.D.
Like I said on Gianni's answer, Comeau is a fairly mainstream compiler that compiles C++ into C.
GMan
@GMan: Well, as you have made me aware of it, I guess in all honesty I have to change my answer a bit. Added a link to it too.
T.E.D.
+7  A: 

Because C compilers are nearly ubiquitous and available on nearly every platform, a lot of (compiled) languages go through this phase in their development to bootstrap the process.

In the early phases of language development to see if the language is feasible the easiest way to get a working compiler out is to build a compiler that converts your language to C then let the native C compiler build the actual binary.

The trouble with this is that language specific constructs are lost and thus potential opportunities for optimization may be missed thus most languages in phase two get their own dedicated compiler front end that understands language specific constructs and can thus provide optimization strategies based on these constructs.

C++ has gone through phase 1 and phase 2 over two decades ago. So it is easy to find a `front end' of a compiler that is dedicated to C++ and generates an intermediate format that is passed directly to a backed. But you can still find versions of C++ that are translated into C (as an intermediate format) before being compiled.

Martin York