Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use make and compile as C99?

Tags:

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?

like image 319
djTeller Avatar asked May 29 '10 12:05

djTeller


2 Answers

The correct way to add compiler flags when compiling modules is by setting the ccflags-y variable. Like this:

ccflags-y := -std=gnu99

See Documentation/kbuild/makefiles.txt in the kernel tree for more information.

Note that I'm using the gnu99 standard instead of c99 since the Linux kernel heavily relies on GNU extensions.

like image 112
mtvec Avatar answered Oct 21 '22 20:10

mtvec


You could just add

CFLAGS=-std=c99

To the top of your makefile, or you can make the code compliant with C90 (as LukeN suggests.)

like image 45
ocodo Avatar answered Oct 21 '22 19:10

ocodo