gpt4 book ai didi

linux-kernel - Linux 内核中使用的是哪个 C 版本?

转载 作者:行者123 更新时间:2023-12-03 08:02:16 27 4
gpt4 key购买 nike

Linux 内核仅使用旧的 C90 语法还是使用 C99/C11 特性进行了优化?
我想知道是否尽可能使用最新版本的 C。

最佳答案

有关更新,请参阅此答案的底部。
Linux kernel coding style文档并没有说太多关于 C90 与 C99 的使用。
它建议将 typedef 用于“在某些特殊情况下与标准 C99 类型相同的新类型”,同时在大多数情况下不鼓励使用 typedef。请注意,这实际上并不意味着取决于 C99 标准,因为此类 typedef 可以在纯 C90 中定义。
在后面对 typedef 的讨论中,它说:

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


这意味着内核必须使用用 C90 编写的用户代码。
对 C99 的唯一其他引用是:

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


顶级 kernel documentation web page将 C99 标准称为“C 编程语言的当前版本”(编写时可能是正确的;当前的正式版本现在是 C11)。
查看内核源码,有1766 Makefile s 在目录树中(截至上次我从 git 中 check out )。其中,只有 3 个引用了 -std=gnu99 gcc 选项,这些用于工具,而不是用于主内核本身(还有 2 个引用 -std=gnu89 ,这是当前的默认值)。这意味着绝大多数 Linux 内核源代码都被编写为使用选项进行编译,这些选项使其(大部分)符合带有一些 GNU 特定扩展的 C89/C90 标准。其中一些扩展是 C99 功能。
Linux 内核的编码约定主要由 Linus Torvalds 控制。 This message of his从 2012 年 4 月开始显示了他对(某些)C99 特定功能的个人态度:
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's answer指出 C99 添加的许多功能都在库中是部分正确的。在大多数情况下,库功能与 Linux 内核无关。内核不在“托管”环境中运行,并且无法访问大部分 C 标准库;例如,您不能使用 printf在内核中(有一个类似的 printk 函数用于记录和调试)。但这只是图片的一部分。 C99 添加的许多功能都使用了语言本身(即 ISO C 标准第 6 节中描述的部分),并且至少可能适用于内核源代码。
更新:
RudolfW 指出 this commit ,这使得 -std=gnu89配置(带有 GNU 扩展的 C 89/90)显式。

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


这是为了响应 gcc 版本 5 中的更改,它更改了 -std=gnu90 中的默认标准选项。至 -std=gnu11 (跳过 -std=gnu99)。顶级 Makefile在 linux git repo 中仍然指的是 -std=gnu89截至 2020 年 9 月 18 日星期五。 ( -std=gnu89-std-gnu90 是等价的。)
Marc.2377's answer引用 a document这可能更直接相关。它明确表示“内核通常在 -std=gnu89 下使用 gcc 编译”。

关于linux-kernel - Linux 内核中使用的是哪个 C 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20600497/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com