2018-09-04 00:32:11 +08:00
|
|
|
.. _programming_language:
|
|
|
|
|
|
|
|
Programming Language
|
|
|
|
====================
|
|
|
|
|
|
|
|
The kernel is written in the C programming language [c-language]_.
|
|
|
|
More precisely, the kernel is typically compiled with ``gcc`` [gcc]_
|
Kbuild: move to -std=gnu11
During a patch discussion, Linus brought up the option of changing
the C standard version from gnu89 to gnu99, which allows using variable
declaration inside of a for() loop. While the C99, C11 and later standards
introduce many other features, most of these are already available in
gnu89 as GNU extensions as well.
An earlier attempt to do this when gcc-5 started defaulting to
-std=gnu11 failed because at the time that caused warnings about
designated initializers with older compilers. Now that gcc-5.1 is
the minimum compiler version used for building kernels, that is no
longer a concern. Similarly, the behavior of 'inline' functions changes
between gnu89 using gnu_inline behavior and gnu11 using standard c99+
behavior, but this was taken care of by defining 'inline' to include
__attribute__((gnu_inline)) in order to allow building with clang a
while ago.
Nathan Chancellor reported a new -Wdeclaration-after-statement
warning that appears in a system header on arm, this still needs a
workaround.
The differences between gnu99, gnu11, gnu1x and gnu17 are fairly
minimal and mainly impact warnings at the -Wpedantic level that the
kernel never enables. Between these, gnu11 is the newest version
that is supported by all supported compiler versions, though it is
only the default on gcc-5, while all other supported versions of
gcc or clang default to gnu1x/gnu17.
Link: https://lore.kernel.org/lkml/CAHk-=wiyCH7xeHcmiFJ-YgXUy2Jaj7pnkdKpcovt8fYbVFW3TA@mail.gmail.com/
Link: https://github.com/ClangBuiltLinux/linux/issues/1603
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Acked-by: Marco Elver <elver@google.com>
Acked-by: Jani Nikula <jani.nikula@intel.com>
Acked-by: David Sterba <dsterba@suse.com>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com>
Reviewed-by: Alex Shi <alexs@kernel.org>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
2022-03-09 05:56:14 +08:00
|
|
|
under ``-std=gnu11`` [gcc-c-dialect-options]_: the GNU dialect of ISO C11.
|
|
|
|
``clang`` [clang]_ is also supported, see docs on
|
|
|
|
:ref:`Building Linux with Clang/LLVM <kbuild_llvm>`.
|
2018-09-04 00:32:11 +08:00
|
|
|
|
|
|
|
This dialect contains many extensions to the language [gnu-extensions]_,
|
|
|
|
and many of them are used within the kernel as a matter of course.
|
|
|
|
|
2020-09-30 05:19:35 +08:00
|
|
|
There is some support for compiling the kernel with ``icc`` [icc]_ for several
|
|
|
|
of the architectures, although at the time of writing it is not completed,
|
|
|
|
requiring third-party patches.
|
2018-09-04 00:32:11 +08:00
|
|
|
|
|
|
|
Attributes
|
|
|
|
----------
|
|
|
|
|
|
|
|
One of the common extensions used throughout the kernel are attributes
|
|
|
|
[gcc-attribute-syntax]_. Attributes allow to introduce
|
|
|
|
implementation-defined semantics to language entities (like variables,
|
|
|
|
functions or types) without having to make significant syntactic changes
|
|
|
|
to the language (e.g. adding a new keyword) [n2049]_.
|
|
|
|
|
|
|
|
In some cases, attributes are optional (i.e. a compiler not supporting them
|
|
|
|
should still produce proper code, even if it is slower or does not perform
|
|
|
|
as many compile-time checks/diagnostics).
|
|
|
|
|
|
|
|
The kernel defines pseudo-keywords (e.g. ``__pure``) instead of using
|
|
|
|
directly the GNU attribute syntax (e.g. ``__attribute__((__pure__))``)
|
|
|
|
in order to feature detect which ones can be used and/or to shorten the code.
|
|
|
|
|
|
|
|
Please refer to ``include/linux/compiler_attributes.h`` for more information.
|
|
|
|
|
|
|
|
.. [c-language] http://www.open-std.org/jtc1/sc22/wg14/www/standards
|
|
|
|
.. [gcc] https://gcc.gnu.org
|
|
|
|
.. [clang] https://clang.llvm.org
|
|
|
|
.. [icc] https://software.intel.com/en-us/c-compilers
|
|
|
|
.. [gcc-c-dialect-options] https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html
|
|
|
|
.. [gnu-extensions] https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html
|
|
|
|
.. [gcc-attribute-syntax] https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html
|
|
|
|
.. [n2049] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2049.pdf
|
|
|
|
|