Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is linux kernel coded using non-standard C (gcc specific features)? [closed]

Tags:

Linux kernel code uses "statement-expression" and typeof extension that makes it only compilable under gcc.

More I think about it, more it doesn't make sense.

It defeats the purpose of portability and standard C. (now linux kernel code needs a specific compiler that supports gcc extensions).

Was it a bad design choice or was there a specific reason for making linux kernel code specific to gcc?

EDIT: When I said it defeats portability, I used it in different context. I was thinking, by conforming to standard C, it would be accepted to ANY compiler that supports standard C (which is exactly the purpose of creating a standard -- to unify all different dialects of C), hence being more portable. Of course, since gcc is so popular, and gcc supports zillion architectures, this line is almost meaningless. I am just asking if there was a specific rationale behind not conforming to standard C.

like image 775
SHH Avatar asked Nov 15 '11 21:11

SHH


People also ask

Is the Linux kernel compiled with GCC?

My non-technical guess: The Linux Kernel can't currently (2009) be compiled with any compiler other than the GNU compiler, gcc.

What C standard does Linux kernel use?

The kernel is written in the C programming language [c-language]. More precisely, the kernel is typically compiled with gcc [gcc] under -std=gnu11 [gcc-c-dialect-options]: the GNU dialect of ISO C11. clang [clang] is also supported, see docs on Building Linux with Clang/LLVM.

What C compiler does Linux use?

GCC, or the GNU Compiler Collection, has been around since the 1980s, predating Linux itself. Not only does it compile C programs, but also handles C++, Objective-C, Objective-C++, Fortran, ADA, and Go. A lot of open-source projects still rely on it, including the Linux kernel.

What is GCC kernel?

Blog, Linux Foundation. As the default compiler for the Linux kernel source, GCC delivers trusted, stable performance and also builds system libraries and many of the applications in popular Linux distributions. Software is useless if computers can't run it.


2 Answers

Why would the Linux kernel developers worry about making their code work on say Microsoft Visual Studio compiler or the IBM xlC compilers?

When you're writing a kernel, you need very precise control over a lot more stuff, like exact memory layout, than you do (generally) in userspace. Such controls are not really accounted for in the C standard (left as implementation defined characteristics for instance), so either some extensions are necessary, or you need to rely on the compiler's quirks.

Sticking with one specific compiler, taking advantage of its extensions, is a rational decision. The code doesn't need to be portable across compilers - it needs to be efficient and portable across different hardware platforms.

like image 188
Mat Avatar answered Sep 18 '22 11:09

Mat


Here's some good background on the specific extensions used. It's not really written from the perspective of "why?", but it should give you some good background on the reasons for choosing this approach:

https://developer.ibm.com/tutorials/l-gcc-hacks/

like image 45
Shaun Avatar answered Sep 22 '22 11:09

Shaun