Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do all C++ compilers generate C code?

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

like image 264
TripShock Avatar asked Jul 22 '10 17:07

TripShock


3 Answers

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.

like image 82
Martin York Avatar answered Oct 07 '22 20:10

Martin York


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.

like image 13
Gianni Avatar answered Oct 07 '22 19:10

Gianni


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.

like image 7
dthorpe Avatar answered Oct 07 '22 20:10

dthorpe