Linux 内核正考虑启用 Microsoft C 扩展。
The Linux Kernel Looks to “Bite the Bullet” in Enabling Microsoft C Extensions

原始链接: https://www.phoronix.com/news/Linux-6.19-Patch-Would-MS-Ext

可能有两个新的补丁会被包含在Linux 6.19内核中,从而启用`-fms-extensions`编译器标志。该标志允许GCC和Clang使用Microsoft C扩展,特别是启用在其他结构体/联合体中匿名包含带标签的结构体/联合体。 虽然之前曾被提出并因担心采用Microsoft特定的行为而否决,但现在的争论点在于启用更简洁的代码和潜在的堆栈空间节省。支持者建议采取“忍痛接受”的方法——普遍启用该标志,以便在有用时可以使用,而不是逐个案例进行论证。 这些补丁确保`-fms-extensions`应用于整个内核构建系统,包括具有自定义编译器标志的架构。值得注意的是,Linus Torvalds尚未表示反对,这表明可能会接受这项更改,尽管可能对将Microsoft C约定合并到主线内核中存在保留意见。

## Linux 内核考虑支持 Microsoft C 扩展 Linux 内核有望在 GNU 编译器集合 (GCC) 中启用 Microsoft C 扩展,这一举动引发了关于标准化和厂商影响的讨论。这些扩展主要解决与 Microsoft 头文件的兼容性问题,特别是允许更灵活的结构体和联合体定义——这些特性已经由 Plan 9 扩展支持,但并非标准 C 的一部分。 争论的焦点在于,即使是非标准特性,采用它们是否值得。一些人认为这是兼容性方面的务实一步,而另一些人则担心偏离既定标准。Linus Torvalds 之前曾反对大端支持,强调了避免没有明确益处的复杂性的偏好。 讨论揭示了一种历史上的紧张关系——有人怀疑过去的反对源于对 Microsoft 的不喜欢,而非技术上的考虑。最终,除非出现意外反对,否则这项更改似乎很可能进行,从而可能简化代码兼容性并简化内核中某些编程模式。此举也凸显了 C 和 C++ 标准的持续演进以及在不同编译器和平台之间保持兼容性的挑战。
相关文章

原文
Two patches queued into the Linux kernel's build system development tree, kbuild-next, would enable the -fms-extensions compiler argument everywhere for allowing GCC and LLVM/Clang to use the Microsoft C Extensions when compiling the Linux kernel. Being in kbuild-next these patches will likely be submitted for the Linux 6.19 kernel merge window next month but remains to be seen if there will be any last minute objections to this change.

The -fms-extensions compiler option honored by the GNU Compiler Collection and LLVM/Clang allow enabling some non-standard C/C++ constructs used within Microsoft header files and honored by the the Microsoft Visual C/C++ compiler. For Linux kernel development purposes, enabling the Microsoft C Extensions would allow including a tagged struct or union anonymously in another struct/union.

Going back many years there have been patches floated to unconditionally enable -fms-extensions for the Linux kernel but they haven't made it past the Linux kernel mailing list. But now with these two patches being in kbuild-next mean that it will likely be submitted for the Linux 6.19 kernel merge window barring any objections from prominent Linux kernel developers or Linus Torvalds himself.

Rasmus Villemoes argued with Kbuild: enable -fms-extensions that would allow for "prettier code" and others have noted in the past the potential for saving stack space and all around being beneficial in being able to leverage the Microsoft C behavior:

"Once in a while, it turns out that enabling -fms-extensions could allow some slightly prettier code. But every time it has come up, the code that had to be used instead has been deemed "not too awful" and not worth introducing another compiler flag for.

That's probably true for each individual case, but then it's somewhat of a chicken/egg situation.

If we just "bite the bullet" as Linus says and enable it once and for all, it is available whenever a use case turns up, and no individual case has to justify it.

A lore.kernel.org search provides these examples:

- https://lore.kernel.org/lkml/[email protected]/
- https://lore.kernel.org/lkml/[email protected]/
- https://lore.kernel.org/lkml/[email protected]/
- https://lore.kernel.org/lkml/[email protected]/
- https://lore.kernel.org/lkml/CAHk-=wjeZwww6Zswn6F_iZTpUihTSNKYppLqj36iQDDhfntuEw@mail.gmail.com/

Undoubtedly, there are more places in the code where this could also be used but where -fms-extensions just didn't come up in any discussion."


The second patch is kbuild: Add '-fms-extensions' to areas with dedicated CFLAGS to ensure -fms-extensions is passed for the CPU architectures that rely on their own CFLAGS being set rather than the main KBUILD_CFLAGS.

Linus Torvalds chimed in on the prior mailing list discussion and doesn't appear to be against enabling -fms-extensions beginning with the Linux 6.19 kernel.

Linux MS C


Enabling -fms-extensions will allow for some better looking C code though some may feel the wrong way around Microsoft C behavior being permitted for the mainline Linux kernel coding.
联系我们 contact @ memedata.com