25

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.

SHH
  • 3,226
  • 1
  • 28
  • 41
  • 10
    Why is it strange that GNU Linux is written to be compiled with the GNU Compiler? Who said that Linux was designed to be ported to other compilers? – Oliver Charlesworth Nov 15 '11 at 21:34
  • 8
    Linux is not about code quality or portability. It's about world domination. – stefan Nov 15 '11 at 21:35
  • 2
    This doesn't sound like it's on-topic for SO. I don't know enough about the scope of [Programmers](http://programmers.stackexchange.com/) to say whether it should be moved there or not. – Josh Darnell Nov 15 '11 at 21:35
  • @Oli Charlesworth [clang](http://clang.llvm.org/) is able to compile a working Linux kernel, and clang isn't a GNU compiler, but I do agree. :) –  Nov 15 '11 at 21:37
  • So, linux was written to be compiled with the GNU compiler because it just wanted people to use GNU compilers? I am just wondering if there was any other performance issues rather than political or social issues. I just thought it would make everything much eaiser if it sticked to standard C (if there is no performance issue). – SHH Nov 15 '11 at 21:37
  • The Intel C compiler also compiles the Linux kernel. – cnicutar Nov 15 '11 at 21:38
  • @WTP I thought clang was all about unimpeachable standards conformance? – Seth Carnegie Nov 15 '11 at 21:39
  • @Seth Carnegie may be, but it has some (if not all) GCC extensions. You can disable them, though. –  Nov 15 '11 at 21:39
  • 11
    @OliCharlesworth The Linux kernel isn't GNU/Linux, GNU/Linux specifies that the core utilities included in a distribution are the GNU implementations. It should be possible to use a Linux kernel with another implementation of the core utilities, e.g. BSD, and that would not be GNU/Linux. – Kevin Nov 15 '11 at 21:40
  • 1
    @cnicutar isn't it because intel c compilers had to put extra efforts to implement gcc extension? – SHH Nov 15 '11 at 21:40
  • 2
    @SHH Once that happens, they're no longer "gcc extensions". They're "stuff needed by some people, implemented by some compilers". Many former gcc extensions are now standard C99 features. – cnicutar Nov 15 '11 at 21:42
  • @SHH: care to explain how using a compiler that runs and generates code for a zillion architectures (approximately) "defeats portability"? – ninjalj Nov 15 '11 at 23:16
  • @ninjalj when I said 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 portability. Of course, since gcc is so popular and and gcc support zillion architectures, this context is almost meaningless. I am just asking was there a specific rationale behind not conforming to standard C. – SHH Nov 16 '11 at 02:54

4 Answers4

36

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • 5
    I don't know what gcc-extension features allow you to precisely control over a lot more stuff like memory layouts or maximizes efficiencies. Isn't that the job of assemly language specific to architecture? I mean if kernel developers wanted maximum efficiencies, why didn't they use assembly languages instead? It looks like any gcc extensions used in kernel could have been replaced by Standard C (correct me if I am wrong). – SHH Nov 15 '11 at 21:55
  • 1
    Writing assembly is much more time-consuming and difficult than writing C. Even in the kernel, it's limited to the areas where either a) it's simply not possible to express in C, or b) for very specific, performance critical pieces of code that the C compiler doesn't get "perfect". Some of the extension used could probably be replaced by standard C, but what would be the point? If it's easier to write with exts, why bother? Also remember that the C standard evolves. Some extensions get implemented by many compilers over time because they are _useful_. Then, eventually, they can become standard – Mat Nov 15 '11 at 22:01
  • 6
    Lastly, and this is _very_ important: the Linux kernel doesn't care about portability _at all_ in the way ordinary applications do. They write the environment they run in and provide to userspace. The portability the kernel cares about is _entirely different_ from the portability of "ordinary" applications. – Mat Nov 15 '11 at 22:03
  • @Mat: the Linux kernel sure cares about portability, that's why it uses a compiler that has been ported and generates code for a zillion architectures. – ninjalj Nov 15 '11 at 23:14
  • 1
    Being limited to gcc is probably a feature rather than a bug, if they had supported other compilers, people will try to compile the kernel under those compilers, and that will exponentially multiply the number of different configurations the kernel can be compiled as as well as other use space programs. The support nightmare probably wouldn't be worth it. – Lie Ryan May 12 '14 at 22:36
  • @ssh, simplest examples would be the `__attribute__`s which don't have a standard equivalent. – Shahbaz Jun 25 '14 at 22:29
  • @SHH: If an extended version of C allows one piece of source code to perform a task on many different architectures that would otherwise require a separate piece of assembly language code for each architecture, why should programmers have to use assembly? I've observed some people who seem reflexively opposed to the use of C for such purposes, but I see no good justification for such opposition. – supercat Feb 10 '16 at 16:12
14

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/

ErrCode
  • 186
  • 2
  • 11
Shaun
  • 2,446
  • 19
  • 33
5

The Linux Kernel code is a complicated piece of software. The more facilities gcc provides them with, they happier the coders would be.

Why would they care about portability? gcc compiles code under virtually every hardware configuration PLUS it provides them with good features. Why would they care if Linux could or could not be compiled with another compiler?

Today, portable code is such a common concept for us that we believe it should be existent everywhere. But that is not the case. For example, if a compiler provides real-time extensions to C, NASA would use it without care for portability. The important point being the features are too good to sacrifice for a portability that is never used (I mean, who would compile the kernel with MS Visual Studio for example?)

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • not every hardware supported by gcc is provided with good features from gcc. – osgx Nov 15 '11 at 21:50
  • What do you mean? The features I had in mind were of the type `"statement-expression" and typeof` as the OP had asked. – Shahbaz Nov 15 '11 at 23:22
  • I mean that gcc don't give good performance to every supported platform. – osgx Nov 16 '11 at 00:27
4

Once the kernel is compiled, it doesn't leave "traces" of its compilation environment around to taint the running kernel experience.

I'd say it's just a matter of expedience. The kernel also contains bits of assembly, and assembly isn't exactly portable either. Perhaps if the "mission" of the kernel was to write a kernel which could be compiled on multiple C compilers, the complaint would fall on more attentive ears.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138