Linux – Does kernel need to be compiled in same dev environment as userspace

gccglibckernellinux-kernel

Lets say my userspace (packages) are compiled with gcc 4.7 and libc6 2.13 (Debian Wheezy)

Can I compile linux kernel under different dev environment, such as gcc 6.3 and libc6 2.24 (ie, under Debian Stretch) ?

I know that unlike packages, kernel is not linked with any dynamic libraries. So theoretically, it should make no difference which gcc and libc it was compiled under.

Is this true ?
Could I run into trouble when I do this ?
Could there perhaps be some incompatibilities caused by different gcc versions?

On the other hand, newer gcc has some interesting features, better security. So perhaps, kernel should be compiled with newest gcc ?

Best Answer

As you point out, the C library being used has no impact on the kernel, the kernel doesn’t use the C library. (There’s an indirect impact, since it’s used to build tools the kernel uses during its build process, but that’s extremely unlikely to affect the end result.)

The kernel can be built with a variety of different compiler versions; according to its documentation, it only needs GCC 3.2 or later. You’ll also find that it can take a while for the kernel to officially support the latest versions of GCC, and longer still for a distribution kernel to use it. For example, the Debian Linux kernel package uses GCC 6, and even has dedicated packages to provide the correct compiler version (linux-compiler-gcc-6-x86 on amd64 and i386). There is no connection between the compiler used for the kernel and the compiler used for userspace (nor is there necessarily any need to use the same compiler for all of userspace — old programs built with GCC 3 or even 2 still work on modern systems).

Newer compiler versions do provide more security features, but GCC 6 is good enough for most if not all the security features used in the kernel.

Related Question