为什么需要将多个源文件编译到一个内核模块中
在开发比较复杂的驱动时为了提高代码的可读性,方便后期维护升级,往往会采用多个源文件来编写一个驱动,此时便涉及到将多个源文件编译成一个内核模块的问题
实现方法
将多个源文件编译成一个内核模块是通过修改makefile实现,在makefile中可以指定将多个源文件生成的.o文件链接成一个.ko文件。
如下是将module_test.c和module_print.c编译在一个merge_module.ko文件中的makefile
ifeq ($(KERNELRELEASE),)
#第一次执行时 KERNELRELEASE 为空,执行此分支
KERNELDIR ?= /home/lf/workspace/source/my_source/linux/linux-5.4.31
ROOTFS ?= /home/lf/workspace/rootfs
PWD := $(shell pwd)
#$(MAKE) 相当于 make
#-C $(KERNELDIR) 执行 KERNELDIR 目录的Makefile
#M=$(PWD) 内核源码树之外的一个目录
#modules 只编译模块
module:
$(MAKE) -C $(KERNELDIR) ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabihf- M=$(PWD) modules
copy:
cp *.ko $(ROOTFS)/root/
install:
$(MAKE) -C $(KERNELDIR) ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabihf- M=$(PWD) INSTALL_MOD_PATH=$(ROOTFS) modules_install
clean:
rm -rf *.o .*.cmd *.mod.* *.mod modules.order Module.symvers .tmp_versions
else
#表示要生成 merge_module.ko
obj-m := merge_module.o
#module.ko 有driver.o和print.o生成
merge_module-objs = module_test.o module_print.o
endif
与将当个源文件编译为一个.ko的makefile相比,其最大区别便是增加了”merge_module-objs = module_test.o module_print.o“一行
代码简介
测试代码如下:
在”module_print.c"中通过了两个函数,分别是“init_print”和“exit_print”,并在"module_print.h"中进行了声明。
在“module_test.c”中提供了模块加载函数和模块卸载函数,并在加载函数中调用了“init_print”,在卸载函数中调用了“exit_print”
实验
- 从“https://gitcode.net/lf282481431/linux_driver_demo/-/tree/master/01-kernel_module/02-module_merge”下载代码,并进行编译,然后拷贝到目标板的root目录中
- 执行“insmod merge_module.ko”命令,此时模块被加载,加载函数在输出"module init\r\n"字符串后又调用“init_print”函数输出"module init call back\r\n"字符串
- 执行“rmmod merge_module.ko”命令,模块被卸载,卸载函数输出"module exit\r\n"字符串后继续调用exit_print函数输出"module exit call back\r\n"字符串