Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the C++ code converted to C during the compilation process? [duplicate]

Tags:

c++

c

compilation

I have read that the original implementation of C++ by Bjarne Stroustrup was using a compiler named Cfront that converted C++ to C during the compilation process.

Is this still the case with modern compilers (most of them ?) ?

I couldn't find a good answer using Google (or I couldn't find the right search terms).

edit: This is not an exact duplicate because I'm asking for current/modern ones. But both questions & answers apply.

like image 312
achedeuzot Avatar asked Jun 11 '14 10:06

achedeuzot


2 Answers

Absolutely not. The CFront way of doing things became untenable long ago. There are some C++ constructs with no C interpretation, especially exceptions, and stamping out literal C source for every template instantiation is a bit ridiculous. The entire reason Bjarne stopped making Cfront is because it was impossible.

It is, however, common to lower the code to a more useful IR like LLVM IR, and GCC also has an internal IR, before converting to machine code.

like image 103
Puppy Avatar answered Sep 21 '22 07:09

Puppy


Short answer: no. Modern C++ compilers generate native code directly.

There's no reason why you can't compile C++ to C, there's just no real reason to do so either any more, so you're adding an extra stage in the compilation process that could just as easily not exist. However, there are still a couple of options if you really need C code output for some reason: the Comeau C++ compiler emits C code with the aim of porting your C++ to platforms where a C++ compiler may not exist (which these days, is very few), and Clang uses LLVM as a backend code generator, which has C as one of its many target instruction languages. (edit: of these options, the first is outdated and the second is no longer maintained)

In neither case does the C look anything like the code you put in: it's significantly less readable than machine code would be. The days of converting method calls to function calls with a this are certainly long gone - it's very much a case of "compiling" rather than "converting".

like image 30
Leushenko Avatar answered Sep 20 '22 07:09

Leushenko