如何使用make和C99进行编译? [英] How to use make and compile as C99?
问题描述
我正在尝试使用Makefile编译linux内核模块:
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
哪个给我:
main.c:54: warning: ISO C90 forbids mixed declarations and code
我需要切换到C99.阅读后,我注意到我需要添加一个标志-std = c99,不确定是否应该将其添加到何处.
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.
如何更改Makefile,使其可以编译为C99?
How do I change the Makefile so it will compile as C99?
推荐答案
与makefile无关. ISO C90禁止在块或文件开头以外的任何地方声明变量-像这样
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
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;
}
因此必须对此进行修改...
Thus it has to be modified to this...
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中.
You can only "fix" that in the source code, not in the makefile.
此规则在C99中已经放宽,但是我认为将变量定义,声明和初始化与下面的代码分开是个好主意:)
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 :)
因此,要更改您的makefile使其可以用C99进行编译,您需要在makefile所引用的"build"目录中更改Makefile,并在"gcc"行中添加"-std = c99"源文件.
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.
这篇关于如何使用make和C99进行编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!