How can the Linux kernel compile itself? How can the Linux kernel compile itself? c c

How can the Linux kernel compile itself?


The first round of binaries for your Linux box were built on some other Linux box (probably).

The binaries for the first Linux system were built on some other platform.

The binaries for that computer can trace their root back to an original system that was built on yet another platform.

...

Push this far enough, and you find compilers built with more primitive tools, which were in turn built on machines other than their host.

...

Keep pushing and you find computers built so that their instructions could be entered by setting switches on the front panel of the machine.

Very cool stuff.

The rule is "build the tools to build the tools to build the tools...". Very much like the tools which run our physical environment. Also known as "pulling yourself up by the bootstraps".


I think you should distinguish between:

compile, v: To use a compiler to process source code and produce executable code [1].

and

install, v: To connect, set up or prepare something for use [2].

Compilation produces binary executables from source code. Installation merely puts those binary executables in the right place to run them later. So, installation and use do not require compilation if the binaries are available. Think about ”compile” and “install” like about “cook” and “serve”, correspondingly.

Now, your questions:

  1. The kernel is written in C, however how did the kernel get compiled without a compiler installed?

The kernel cannot be compiled without a compiler, but it can be installed from a compiled binary.

Usually, when you install an operating system, you install an pre-compiled kernel (binary executable). It was compiled by someone else. And only if you want to compile the kernel yourself, you need the source and the compiler, and all the other tools.

Even in ”source-based” distributions like gentoo you start from running a compiled binary.

So, you can live your entire life without compiling kernels, because you have them compiled by someone else.

  1. If the C compiler is installed on my machine before the kernel is compiled, how can the compiler itself get compiled without a compiler installed?

The compiler cannot be run if there is no kernel (OS). So one has to install a compiled kernel to run the compiler, but does not need to compile the kernel himself.

Again, the most common practice is to install compiled binaries of the compiler, and use them to compile anything else (including the compiler itself and the kernel).

Now, chicken and egg problem. The first binary is compiled by someone else... See an excellent answer by dmckee.


The term describing this phenomenon is bootstrapping, it's an interesting concept to read up on. If you think about embedded development, it becomes clear that a lot of devices, say alarm clocks, microwaves, remote controls, that require software aren't powerful enough to compile their own software. In fact, these sorts of devices typically don't have enough resources to run anything remotely as complicated as a compiler.

Their software is developed on a desktop machine and then copied once it's been compiled.

If this sort of thing interests you, an article that comes to mind off the top of my head is: Reflections on Trusting Trust (pdf), it's a classic and a fun read.