如何使用 -std=gnu99 编译 Linux 内核模块? [英] How to compile a Linux kernel module using -std=gnu99?
问题描述
我最近学习了如何编写简单的字符驱动程序,在尝试编写代码时,我注意到我的 C99 代码收到了很多以下 GCC 警告:
I've recently learned how to program simple character drivers and while playing around with the code I noticed that I get a lot of the following GCC warnings thrown for my C99 code:
warning: ISO C90 forbids mixed declarations and code
我认为这是因为主 Linux 内核 Makefile 设置为使用非 C99 标准进行编译.我四处搜索我在stackoverflow上找到了这个答案:How使用 make 并编译为 C99?
I assume this is because the main Linux kernel Makefile is set to compile using a non-C99 standard. I searched around I found this answer here on stackoverflow: How to use make and compile as C99?
所以我很自然地在我的 Makefile 中尝试了以下内容:
So I naturally tried the following in my Makefile:
ccflags-y := -std=gnu99
不幸的是,这并没有消除 GCC 警告.我检查了 make
的详细输出,并验证了 GCC 确实是在最后添加了 -std=gnu99
的情况下执行的;所以我有点困惑.
Unfortunately this didn't silence the GCC warnings. I checked the verbose output of make
and verified that GCC is indeed executed with the -std=gnu99
tacked on at the end; so I'm a bit confused.
如何使用 -std=gnu99
选项正确编译 Linux 内核模块?
How do I properly compile a Linux kernel module using the -std=gnu99
option?
编辑:
我注意到 GCC 输出显示了这个选项:-Wdeclaration-after-statement
.这就是为什么即使使用 -std=gnu99
选项我也会收到警告?
I noticed the GCC output shows this option: -Wdeclaration-after-statement
. Is this why I am getting the warnings even with the -std=gnu99
option?
推荐答案
事实证明 -std=gnu99
确实 确实有效;删除编译器标志后,我开始看到有关 C99 功能的错误.所以这意味着除了 -std=
选项之外,还有其他原因导致警告打印出来.
It turns out that -std=gnu99
does in fact work; I began seeing errors regarding C99 features after removing the compiler flag. So that meant something else was causing the warnings to print out besides the -std=
option.
在通过 make V=1
解析详细输出后,我发现 -Wdeclaration-after-statement
选项是 GCC 执行的一部分.这就是我看到的 ISO C90 混合声明警告的原因.
After parsing through the verbose output via make V=1
, I discovered the -Wdeclaration-after-statement
option as part of the GCC execution. This was the cause of the ISO C90 mixed declaration warnings I saw.
要禁用 ISO C90 警告,请将其传递给 GCC:-Wno-declaration-after-statement
.
To disable the ISO C90 warnings, pass this to GCC: -Wno-declaration-after-statement
.
例如:
ccflags-y := -std=gnu99 -Wno-declaration-after-statement
这篇关于如何使用 -std=gnu99 编译 Linux 内核模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!