如何使用-std = gnu99编译Linux内核模块?

Vil*_*ray 10 c linux gcc c99 linux-kernel

我最近学会了如何编写简单的字符驱动程序,在玩这些代码时,我注意到我的C99代码中出现了很多以下的GCC警告:

warning: ISO C90 forbids mixed declarations and code
Run Code Online (Sandbox Code Playgroud)

我假设这是因为主Linux内核Makefile设置为使用非C99标准进行编译.我在附近搜索我在stackoverflow上找到了这个答案:如何使用make和编译为C99?

所以我自然在我的Makefile中尝试了以下内容:

ccflags-y := -std=gnu99
Run Code Online (Sandbox Code Playgroud)

不幸的是,这并没有使GCC警告无声.我检查了详细的输出make并验证了GCC确实-std=gnu99在最后加上了; 所以我有点困惑.

如何使用该-std=gnu99选项正确编译Linux内核模块?

编辑:

我注意到GCC输出显示了这个选项:-Wdeclaration-after-statement.这是为什么即使有-std=gnu99选项我也会得到警告?

Vil*_*ray 19

事实证明,事实上-std=gnu99 确实有效; 删除编译器标志后,我开始看到有关C99功能的错误.所以这意味着除了-std=选项之外还有其他东西导致警告打印出来.

在解析了详细输出后make V=1,我发现该-Wdeclaration-after-statement选项是GCC执行的一部分.这是我看到的ISO C90混合声明警告的原因.

要禁用ISO C90警告,请将其传递给GCC : -Wno-declaration-after-statement.

例如:

ccflags-y := -std=gnu99 -Wno-declaration-after-statement
Run Code Online (Sandbox Code Playgroud)