方式一:
当linux内核驱动模块代码位于内核源码目录树外部时,以helloworld模块为例,Makefile 写法如下:
$(warning KERNELRELEASE=$(KERNELRELEASE))
ifeq ($(KERNELRELEASE),)
#KERN_DIR = /home/huang/kernel3.10/linux-3.10.0-1160
#KERN_VER = $(shell uname -r)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* modules*
.PHONY: modules modules_install clean
else
obj-m := helloword.o
endif
方式二:
当helloworld位于linux内核源码目录下(假设位于:linux-3.10/char/hellowarld/)时,只需要在目录下添加如下Makefile即可
obj-m := helloword.o
#obj-${CONFIG_HELLO_WORLD} := helloword.o
如果helloword有多个源码文件,可以改成如下:
obj-$(CONFIG_HELLO_WORLD) += helloword.o
helloworld-objs := helloword-1.o helloword-2.o helloword-3.o
还要在helloword上一层(linux-3.10/char)Makefile 中要将helloworld 目录包含进来
obj-m += helloworld
# obj-${CONFIG_HELLO_WORLD} += helloworld
本文介绍了两种在Linux环境下编译内核模块的方法。一种是当模块代码位于内核源码目录外部时的编译流程;另一种是当模块位于内核源码目录内部时的编译方法。文中详细展示了两种情况下的Makefile配置。
771

被折叠的 条评论
为什么被折叠?



