Eg: a common device module's Makefile
obj-m:=jc.o
default:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules clean
I consider if I can set CFLAGS to the file. When I change default section to
$(MAKE) -O2 -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
But it didn't work.
Any help? Thanks a lot.
-O2
would be an option tomake
(or$(MAKE)
, as you're using it) in what you tried. Obviously, the compiler (probablygcc
) needs this flag, notmake
.Kbuild understands a make variable named
CFLAGS_modulename.o
to add specific C flags when compiling this unit. In your case, your module object will bejc.o
, so you can specify:and it should work. Add
V=1
to your$(MAKE)
lines to get a verbose output and you should see-O2
whenjc.c
is being compiled.You can find more about compiling modules in the official documentation.
You can also use
This will be applied to all of the source files compiled for your module with the Makefile. This is indirectly documented in the link provided by eepp in Section 4.2