What GNU C extensions are available that aren't trivial to implement in C99?

5.2k Views Asked by At

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

4

There are 4 best solutions below

0
On

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.

0
On

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.

0
On

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.

1
On

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 architecture dependent, or is made possible because of how gcc is implemented, and probably do 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 have to provide the same extensions as gcc to be able to build the kernel.

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