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?
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.You could just add
To the top of your
makefile
, or you can make the code compliant with C90 (as LukeN suggests.)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.