54

Stroustrup claims that Cfront, the first C++ compiler, was written in C++ (Stroustrup FAQ).

However, how is it even possible that the first C++ compiler be written in C++?

The code that makes up the compiler needs to be compiled too, and thus the first C++ compiler couldn't have been written in C++, could it?

gnat
  • 20,543
  • 29
  • 115
  • 306
Pacerier
  • 5,053

5 Answers5

64

The key is right here:

The first C++ compiler (Cfront) was written in C++. To build that, I first used C to write a "C with Classes"-to-C preprocessor. "C with Classes" was a C dialect that became the immediate ancestor to C++. That preprocessor translated "C with Classes" constructs (such as classes and constructors) into C. It was a traditional preprocessor that didn't understand all of the language, left most of the type checking for the C compiler to do, and translated individual constructs without complete knowledge. I then wrote the first version of Cfront in "C with Classes".

So the first version of Cfront wasn't written in C++, rather in the intermediate language. The ability to create C compilers and preprocessors directly in C led to many of the innovations (and massive security holes) in C. So you write your new preprosessor that turns your "C with Classes" code into straight C (because straight C can do anything) and then you use "C with Classes" to write a C++ compiler (not that you couldn't do it in C, just it would take awhile) and then you use that C++ compiler to write a more effecient/complete compiler in C++. Got it?

Christopher Bibbs
  • 2,749
  • 16
  • 12
17

It was bootstrapped. As soon as a C++ feature was added to cfront, then cfront could also use that feature from that point on (but not to implement that very feature). This worked because cfront had the ability to convert C++ code to C code. So if some new platform came out, you could use cfront on another platform to convert cfront from C++ to C, and then use the new platform's C compiler to finish the compilation from C to object code.

9

I think B.S. answers that question:

The first C++ compiler (Cfront) was written in C++. To build that, I first used C to write a "C with Classes"-to-C preprocessor. "C with Classes" was a C dialect that became the immediate ancestor to C++. That preprocessor translated "C with Classes" constructs (such as classes and constructors) into C. It was a traditional preprocessor that didn't understand all of the language, left most of the type checking for the C compiler to do, and translated individual constructs without complete knowledge.

I then wrote the first version of Cfront in "C with Classes". Cfront was a traditional compiler that did complete syntax and semantic checking of the C++ source. For that, it had a complete parser, built symbol tables, and built a complete internal tree representation of each class, function, etc. It also did some source level optimization on its internal tree representation of C++ constructs before outputting C. The version that generated C, did not rely on C for any type checking. It simply used C as an assembler. The resulting code was uncompromisingly fast.

First he created something he called "C with Classes" implemented by a simple preprocessor into C. It was basically C++, but the preprocessor did little or no checking. He then used that to write Cfront, the more powerful version of the translator of C++ into C, complete with type checking, symbol tables, etc.

Mike Dunlavey
  • 12,905
3

I'll add this answer since no answer covered this aspect.

You technically don't need software to compile code. As long as you have the necessary compiler specifications you can do the actual compilation manually. This is not how the first C++ compiler was compiled. I'm just saying it's possible.

Compare with assembly language. When they were used in the early days, there were no assembler software to convert the assembly code to machine code. It was done by hand, but the assembly language gave the programmers a better overview.

klutt
  • 1,438
-1

A Computer is like a king or queen who is fluent in machine language, but has to ask translators, assemblers, and interpreters to translate languages like C++ into machine language for the reigning monarchs ears.

Computers do not directly understand C++. When compiling your code into machine language, we can talk about the following:

  1. The source language
  2. The language in which the compiler is written.
  3. The target language

A C++ compiler compiles C++ into machine language.

The source language is C++.

The destination language is machine language.

The language in which the compiler is written can be anything available which you enjoy using.

The compiler can be written in python, FORTRAN, Intel x86, or anything that you desire.

The compiler essentially takes a text file as input and outputs a executable binary file (a file written in machine language).

If you make many many new versions of the same language, then you can compile newer versions using older versions.

Originally, people:

  1. compiled C++ into C
  2. compiled C into x86 Assembly
  3. assembled x86 Assembly into a machine language.

Eventually, people started skipping steps in the middle. For example, some code written in C++ is never translated into C before it becomes an executable binary file.

Feel free to edit the following table to make it more historically accurate.

Someone else will pick up where you left off if you get tired.

SOURCE LANGUAGE LANGUAGE OF THE COMPILER DESINATION LANGUAGE
C++ version 1.0 C C
C++ version 2.0 C++ version 1.0 C
C++ version 3.0 C++ version 2.0 C
C++ version 4.0 C++ version 3.0 C