Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GCC compile itself 3 times?

I have compiled GCC from source but I can't seem to fully understand the utility of gcc compiling itself three times.

What benefit does this serve ?

This answer says:

  • Build new version of GCC with existing C compiler
  • re-build new version of GCC with the one you just built
  • (optional) repeat step 2 for verification purposes.

Now my question is that once the first step is complete and the compiler is built why waste time rebuilding it ?

Is it just for verification ? If so, it seems pretty wasteful.

Things get more complicated over here,

The build for this is more complex than for prior packages, because you’re sending more information into the configure script and the make targets aren’t standard.

I mean the whole compiler is written in C right, so why not just do everything in one pass ?

What is the use of the 3-phase bootstrap ?

Thanks in advance.

like image 598
ng.newbie Avatar asked Mar 06 '20 15:03

ng.newbie


People also ask

How long does GCC take to compile?

You typically don't want to mess the system's default GCC because other packages may depend on the default version. Depending on the speed of your computer the build phase could take from about 30 minutes to a few hours.

Is C++ bootstrapped?

It was implemented in C++ but translated by what Stroustrup calls a "preprocessor" from C++ to C; not a full compiler by his definition, but still C++ was bootstrapped in C. The 3-step version of the bootstrap build process is indeed for verification: the compiler itself is used as its own test case.

Is G++ written in C++?

G++ builds object code directly from your C++ program source. There is no intermediate C version of the program. (By contrast, for example, some other implementations use a program that generates a C program from your C++ source.)


1 Answers

  • Stage 2. and 3. are a good test for the compiler itself: If it can compile itself (and usually also some libraries like libgcc and libstdc++-v3) then it can chew non-trivial projects.

  • In stage 2. and 3., you can generate the compiler different options, for example without optimization (-O0) or with optimization on (-O2). As the output / side effects of a program should not depend on the optimization level used, either version of the compiler must produce same binaries, even though they are binary very different. This is yet another (run-time test) for the compiler.

If you prefer non-bootstrap for some reason, configure --disable-bootstrap.

like image 78
emacs drives me nuts Avatar answered Oct 05 '22 19:10

emacs drives me nuts