如何像C99一样使用make和编译?
我正在尝试使用 Makefile 编译 Linux 内核模块:
obj-m += main.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
这给了我:
main.c:54: warning: ISO C90 forbids mixed declarations and code
我需要切换到 C99。读完后我注意到我需要添加一个标志 -std=c99,不确定应该添加到哪里。
如何更改 Makefile 以便将其编译为 C99?
I'm trying to compile a linux kernel module using a Makefile:
obj-m += main.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Which gives me:
main.c:54: warning: ISO C90 forbids mixed declarations and code
I need to switch to C99. After reading I noticed I need to add a flag -std=c99, not sure where it suppose to be added.
How do I change the Makefile so it will compile as C99?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编译模块时添加编译器标志的正确方法是设置 ccflags-y 变量。像这样:
请参阅 Documentation/kbuild/makefiles.txt在内核树中获取更多信息。
请注意,我使用的是
gnu99
标准而不是c99
,因为 Linux 内核严重依赖于 GNU 扩展。The correct way to add compiler flags when compiling modules is by setting the
ccflags-y
variable. Like this:See Documentation/kbuild/makefiles.txt in the kernel tree for more information.
Note that I'm using the
gnu99
standard instead ofc99
since the Linux kernel heavily relies on GNU extensions.您可以只添加
到
makefile
的顶部,或者您可以使代码兼容 C90(如 LukeN 建议的那样。)You could just add
To the top of your
makefile
, or you can make the code compliant with C90 (as LukeN suggests.)和makefile没关系。 ISO C90 禁止在块或文件开头以外的任何地方声明变量 - 像这样
因此它必须修改为这样...
您只能在源代码中“修复”它,而不能在 makefile 中“修复”它。
这条规则在 C99 中已经放宽,但在我看来,将变量定义、声明和初始化与其下面的代码分开是个好主意:)
因此,要更改 makefile 以使其使用 C99 进行编译,您需要更改 Makefile makefile 引用的“build”目录,并在编译源文件的“gcc”行添加“-std=c99”。
It's got nothing to do with the makefile. ISO C90 forbids declaring variables anywhere but in the beginning of a block or the file - like this
Thus it has to be modified to this...
You can only "fix" that in the source code, not in the makefile.
This rule has been relaxed in C99, but in my opinion it's a good idea to separate variable definitions, declarations and initializations from the code below it :)
So to change your makefile to make it compile with C99, you need to change the Makefile in the "build" directory that your makefile is referencing, and add the "-std=c99" at the "gcc" line compiling the source file.