views:

129

answers:

5

How come the Linux kernel can compile only with GCC? What GNU C extensions are really necessary for some projects and why?

+1  A: 

GCC supports Nested Functions, which are not part of the C99 standard. That said, some analysis is required to see how prevalent they actually are within the linux kernel.

Justin Ethier
A: 

I guess it's not they are really that necessary. Just there are many useful ones and cross-compiler portability is not that much an issue for Linux kernel to forgo the niceties. Not to mention sheer amount of work it would take to get rid of relying on extensions.

doublep
+4  A: 

This article explains the extensions used: GCC hacks in the Linux kernel. Some of them are trivial, some are not (mostly the optimization tricks).

liori
+1  A: 

Linux kernel was written to be compiled by GCC, so standard compliance was never an objective for kernel developers.

And if GCC offers some useful extensions that make coding easier or compiled kernel smaller or faster, it was a natural choice to use these extensions.

qrdl
+4  A: 

Here's a couple gcc extensions the linux kernel uses:

  • inline assembly
  • gcc builtins, such as __builtin_expect,__builtin_constant,__builtin_return_address
  • function attributes to specify e.g. what registers to use (e.g. _attribute_((regparm(0)),_attribute_((packed, aligned(PAGE_SIZE))) ) )
  • specific code depending on gcc predefined macros (e.g. workarounds for certain gcc bugs in certain versions)
  • ranges in switch cases (case 8 ... 15:)

Here's a few more: http://www.ibm.com/developerworks/linux/library/l-gcc-hacks/

Much of these gcc specifics are very architectore dependant, or is made possible beacuse of how gcc is implemented - and probably does not make sense to be specified by a C standard. Others are just convenient extensions to C. As the linux kernel is built to rely on these extensions - other compilers are givcen little concern - and have to provide the same extensions as gcc to be able to build the kernel.

It's not that they had to rely on these features of gcc, e.g. the NetBSD kernel relies very little on gcc specific stuff.

nos