Linux 内核中使用的是哪个 C 版本? [英] Which C version is used in the Linux kernel?

查看:20
本文介绍了Linux 内核中使用的是哪个 C 版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux 内核是只使用旧的 C90 语法还是使用 C99/C11 特性进行了优化?

Does the Linux kernel use only the old C90 syntax or has it been optimized with C99 / C11 features?

我想知道是否尽可能使用最新版本的 C.

I was wondering if the newest versions of C are used when possible.

推荐答案

请参阅此答案底部的更新.

Linux 内核编码风格文档没有说很多关于 C90 和 C99 的使用.

The Linux kernel coding style document doesn't say much about the use of C90 vs. C99.

它建议将 typedef 用于在某些特殊情况下与标准 C99 类型相同的新类型";虽然在大多数情况下不鼓励使用 typedef.请注意,这实际上并不意味着取决于 C99 标准,因为此类 typedef 可以在纯 C90 中定义.

It suggests the use of typedefs for "New types which are identical to standard C99 types, in certain exceptional circumstances" while discouraging typedefs in most cases. Note that this doesn't actually imply depending on the C99 standard, since such typedefs can be defined in pure C90.

在后面对 typedef 的讨论中,它说:

Later in the discussion of typedefs, it says:

在用户空间可见的某些结构中,我们不能需要 C99 类型,不能使用上面的 u32 形式.因此,我们使用__u32 和所有结构中的类似类型共享用户空间.

In certain structures which are visible to userspace, we cannot require C99 types and cannot use the u32 form above. Thus, we use __u32 and similar types in all structures which are shared with userspace.

这意味着内核必须使用用 C90 编写的用户代码.

The implication is that the kernel must work with user code written in C90.

对 C99 的唯一引用是:

The only other reference to C99 is:

Linux 风格的注释是 C89 /* ... */";风格.
不要使用 C99 风格的//...";评论.

Linux style for comments is the C89 "/* ... */" style.
Don't use C99-style "// ..." comments.

顶级内核文档网页将 C99 标准称为当前版本"C 程序设计语言"(写的时候大概是对的,现在的正式版本是C11)

The top-level kernel documentation web page refers to the C99 standard as the "current version of the C programming language" (which was probably correct when it was written; the current official version is now C11).

查看内核源代码,目录树中有 1766 个 Makefile(截至我上次从 git 中检出).其中,只有 3 个引用了 -std=gnu99 gcc 选项,这些用于工具,而不是主内核本身(还有 2 个引用 -std=gnu89,这是当前的默认设置).这意味着绝大多数 Linux 内核源代码都被编写为使用选项进行编译,这些选项使其(大部分)符合 C89/C90 标准以及一些特定于 GNU 的扩展.其中一些扩展是 C99 特性.

Looking at the kernel sources, there are 1766 Makefiles in the directory tree (as of the last time I checked it out from git). Of these, only 3 refer to the -std=gnu99 gcc option, and those are for tools, not for the main kernel itself (and 2 more refer to -std=gnu89, which is currently the default). This implies that the vast majority of the Linux kernel sources are written to be compiled with options that cause it to (mostly) conform to the C89/C90 standard with some GNU-specific extensions. Some of these extensions are C99 features.

Linux 内核的编码约定主要由 Linus Torvalds 控制.他在 2012 年 4 月的这条消息 显示了他对(某些)C99 的个人态度-具体功能:

The coding conventions for the Linux kernel are largely controlled by Linus Torvalds. This message of his from April 2012 shows his personal attitude regarding (some) C99-specific features:

On Wed, Apr 11, 2012 at 9:28 PM, Oleg Nesterov <oleg@redhat.com> wrote:
>
> Agreed. But,
>
>        error: 'for' loop initial declaration used outside C99 mode
>
> we should change CFLAGS, I guess. BTW, personally I'd like very much
> to use "for (type var; ...")" if this was allowed.

The sad part is that if we allow that, we also get that *other* insane
C99 variable thing - mixing variables and code.

I *like* getting warnings for confused people who start introducing
variables in the middle of blocks of code. That's not well-contained
like the loop variable.

That said, most of the stuff in C99 are extensions that we used long
before C99, so I guess we might as well just add the stupid flag. And
discourage people from mixing declarations and code other ways (sparse
etc).

                         Linus

Bandrami 的回答部分正确指出 C99 添加的许多功能在图书馆.在大多数情况下,库功能与 Linux 内核无关.内核不在托管"环境中运行.环境,并且它无法访问大部分 C 标准库;例如,您不能在内核中使用 printf(有一个类似的 printk 函数用于日志记录和调试).但这只是图片的一部分.C99 添加的许多功能都使用了语言本身(即 ISO C 标准第 6 节中描述的部分),并且至少可能适用于内核源代码.

Bandrami's answer is partially correct in pointing out that many of the feature added by C99 are in the library. For the most part, library features are irrelevant to the Linux kernel. The kernel does not run in a "hosted" environment, and it doesn't have access to most of the C standard library; for example, you can't use printf in the kernel (there's a similar printk function used for logging and debugging). But that's only part of the picture. Many of the features added by C99 are in the language proper (i.e., the part described by section 6 of the ISO C standard), and are at least potentially applicable to kernel source code.

更新:

RudolfW 指出这个提交,这使得 -std=gnu89 配置(带有 GNU 扩展的 C 89/90)显式.

RudolfW points out this commit, which makes the -std=gnu89 configuration (C 89/90 with GNU extensions) explicit.

最终结果:我们可能能够升级到更新的 stdc 模型最终,但现在较新的模型有一些烦人的不足之处,所以传统的gnu89"模型最终成为首选.

End result: we may be able to move up to a newer stdc model eventually, but right now the newer models have some annoying deficiencies, so the traditional "gnu89" model ends up being the preferred one.

这是为了响应 gcc 版本 5 的更改,它将默认标准选项从 -std=gnu90 更改为 -std=gnu11(跳过 -std=gnu99).截至 2020 年 9 月 18 日星期五,linux git repo 中的顶级 Makefile 仍然指的是 -std=gnu89.(-std=gnu89-std-gnu90 是等价的.)

This was in response to a change in gcc release 5, which changed the default standard option from -std=gnu90 to -std=gnu11 (skipping -std=gnu99). The top-level Makefile in the linux git repo still refers to -std=gnu89 as of Fri 2020-09-18. (-std=gnu89 and -std-gnu90 are equivalent.)

Marc.2377 的回答 引用了 一个可能更直接相关的文档.它明确表示内核通常在 -std=gnu89 下使用 gcc 编译".

Marc.2377's answer cites a document that's probably more directly relevant. It explicitly says that "the kernel is typically compiled with gcc under -std=gnu89".

这篇关于Linux 内核中使用的是哪个 C 版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆