如何使用制造和编译为C99?
题
我正在尝试使用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。阅读后,我注意到我需要添加一个flag -std = c99,不确定要添加它的位置。
如何更改Makefile,以将其编译为C99?
解决方案
这与makefile无关。 ISO C90禁止在块或文件开头以外的任何地方声明变量 - 像这样
int main(int argc, char **argv) {
int a; /* Ok */
int b = 3; /* Ok */
printf("Hello, the magic number is %d!\n", b);
int c = 42; /* ERROR! Can only declare variables in the beginning of the block */
printf("I also like %d.. but not as much as %d!\n", c, b);
return 0;
}
因此,必须对此进行修改...
int main(int argc, char **argv) {
int a; /* Ok */
int b = 3; /* Ok */
int c = 42; /* Ok! */
printf("Hello, the magic number is %d!\n", b);
printf("I also like %d.. but not as much as %d!\n", c, b);
return 0;
}
您只能在源代码中而不是在makefile中“修复”。
该规则已在C99中放松,但是我认为将可变定义,声明和初始化与下面的代码分开是一个好主意:)
因此,要更改makefile以使其与C99进行编译,您需要在“构建”目录中更改MakeFile正在引用的“构建”目录中的换档,并在“ GCC”行中添加“ -STD = C99”行以编译源文件。
其他提示
编译模块时,添加编译器标志的正确方法是通过设置 ccflags-y
多变的。像这样:
ccflags-y := -std=gnu99
看 文档/kbuild/makefiles.txt 在内核树中以获取更多信息。
请注意,我正在使用 gnu99
标准而不是 c99
由于Linux内核在很大程度上依赖GNU扩展。
你可以添加
CFLAGS=-std=c99
到你的顶部 makefile
, ,或者您可以使代码符合C90(如Luken所建议)。
不隶属于 StackOverflow