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

查看:451
本文介绍了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.

建议对在某些特殊情况下与标准C99类型相同的新类型"使用typedef,同时在大多数情况下不鼓励使用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.

这是对gcc5更改的回应,该更改显然将默认标准更改为C11(我不知道这是-std=c11还是-std=gnu11,尽管如果不是后者,我会有些惊讶)

This was in response to a change in gcc5, which apparently changes the default standard to C11 (I don't know whether that's -std=c11 or -std=gnu11, though I"d be a little surprised if it's not the latter.)

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

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