13

Since 4.8 release, the C++ compiler GCC (the G++ part of it) is written not in C anymore, but in C++ itself. I have a hypothetical question on this.

I wonder how to compile the C++ code of GCC on a new platform that has no C++ compiler yet. Of course, you could use prebuilt binaries compiled on other machines. Or you could use an older version of GCC that was written in C and compile the current version with it.

However, without prebuilt binaries and just the newest version, you were stuck, right? If not, are there other implications on this situation raised by the switch from C to C++ of the GCC project?

danijar
  • 826
  • 1
  • 10
  • 16
  • 4
    I'm not sure what you are asking here, though you may wish to read about [cross compilation](http://en.wikipedia.org/wiki/Cross_compiler#GCC_and_cross_compilation). –  Dec 12 '14 at 02:01
  • True, if you had just the latest GCC source but no C++ compiler binaries, you wouldn't be able to build it. If you had the GCC source's history, you could go back to the latest C version and use that to build the latest... – Daniel Lubarov Dec 12 '14 at 02:02
  • 5
    Entirely new platform will not have C++ compiler nor C compiler. Cross compilation is the answer. – mip Dec 12 '14 at 02:23
  • 1
    You should probably read [GCC's move to C++](http://lwn.net/Articles/542457/), [Moving to C++](http://petereisentraut.blogspot.com/2013/05/moving-to-c.html) and possibly the discussion [GCC will now need C++ to build](http://www.reddit.com/comments/y9hv0/gcc_will_now_need_c_to_build/) on reddit. I am not sure that `gcc` prior to 4.8 could be built with the C compiler packaged with an arbitrary operating system (and that would restrict you to C89 on many). –  Dec 12 '14 at 02:29
  • 3
    I don't get what's special about C++ for this question, the problem exists with C or any other language, doesn't it? – RemcoGerlich Dec 12 '14 at 09:25
  • 1
    Yes, but chickens and eggs have the same problem and they've solved it too. – psr Dec 12 '14 at 17:47
  • 2
    Possible duplicate of [How could the first C++ compiler be written in C++?](http://programmers.stackexchange.com/questions/105313/how-could-the-first-c-compiler-be-written-in-c) – gnat Aug 23 '16 at 00:16

3 Answers3

22

This is actually a well-known concept called bootstrapping. Basically, there exists, somewhere, a minimal C codebase to build a version of GCC that's capable of building the current GCC codebase. Self-hosting languages have been doing things like that for decades.

danijar
  • 826
  • 1
  • 10
  • 16
Mason Wheeler
  • 82,151
  • 24
  • 234
  • 309
  • Actually, no. That's not the case anymore (and the issue with the question). `gcc` can't be build by a c only compiler anymore. The only guarantee with gcc compiles that they make is that gcc version N can be built with gcc version N-1. –  Dec 12 '14 at 03:00
  • 10
    @MichaelT: But an earlier version of GCC *can* be built with a C compiler, which can then compile later versions written in C++, which is what I said. – Mason Wheeler Dec 12 '14 at 03:16
  • I'd also point to the questions in the question: `However, without prebuilt binaries and just the newest version, you were stuck, right? If not, are there other implications on this situation raised by the switch from C to C++ of the GCC project?` - it presupposes you *don't* have access to previous versions nor does it address the other implications of the switch from C to C++ for the codebase. –  Dec 12 '14 at 05:03
12

Creating a compiler that is written in the same language that it compiles is called bootstrapping. The wikipedia article describes a number of ways that a compiler can be bootstrapped.

Given your restriction that you only have a post-4.8 G++ source code and no pre-built binaries for your target platform (no existing C++ compiler), then bootstrapping the G++ compiler can be done by means of cross-compilation.

When bootstrapping a compiler using cross-compilation, you build several versions of your compiler

  1. On your PC, you install a C++ compiler (can be any C++ compiler, doesn't have to be G++)
  2. Using that compiler, you create a G++ cross-compiler that can execute on the PC and generates code for the target platform
  3. Using the G++ cross-compiler you just built, you create a native G++ compiler that can run on the target platform and create code for it.
  4. You are done. You have created a C++ compiler for the new platform.

If you also don't have a PC (or similar) to perform the initial steps on, then you are indeed stuck, but the chance of being in that situation and trying to bootstrap a compiler are negligible.

Bart van Ingen Schenau
  • 71,712
  • 20
  • 110
  • 179
2

What would happen if some virus managed to delete all existing compiled C++ compilers, including all backups, and all C++ compilers were written in C++? We would still have source code to C++ compilers.

We would have a problem. We couldn’t compile any C++ code whatsoever because we have no compiled C++ compilers. We couldn’t build any of the C++ compilers that we have source code for because they are all written in C++. What would we do?

We would look for the best compiler that we can find. Maybe we find a compiled C or C# compiler. We could use that to compile and run C or C# code. We would then take our C++ source code for a C++ compiler, translate it manually to C or C#, compile it, and use that to compile the C++ source code.

We would have left out any code for optimising C++ code, or for compiling C++ features not used by the C++ compiler in our C or C# version, so the result wouldn’t be a good or complete C++ compiler. But it would be good enough to compile our C++ compiler, so we’d have a slow (unoptimised) compiler for the complete language. We compile our C++ compiler source code again with this version, and now we have our original compiler back.

gnasher729
  • 42,090
  • 4
  • 59
  • 119