Is it possible to set CFLAGS to a linux kernel module Makefile?

早过忘川 提交于 2019-11-26 16:27:27

问题


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.


回答1:


-O2 would be an option to make (or $(MAKE), as you're using it) in what you tried. Obviously, the compiler (probably gcc) needs this flag, not make.

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 be jc.o, so you can specify:

CFLAGS_jc.o := -O2

and it should work. Add V=1 to your $(MAKE) lines to get a verbose output and you should see -O2 when jc.c is being compiled.

You can find more about compiling modules in the official documentation.




回答2:


You can also use

ccflags-y := -O2

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



来源:https://stackoverflow.com/questions/18478859/is-it-possible-to-set-cflags-to-a-linux-kernel-module-makefile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!