独立编译Ko文件添加方式1.创建文件夹test,其中包含test.c与编译规则Makefile文件:
2.在test.c中编写:
#include
test@test:~/source$ cat ./build/configs/x2600e_vast_v20_nand_5.10_factory_defconfig | grep kernel APP_kernel_dir=../kernel/kernel //获取kernel路径APP_kernel_config=x2600_module_base_linux_sfc_nand_defconfig KERN_DIR = /home/your_linux/kernel/kernel #获取到的kernel绝对路径obj-m += test.o #我们需要编译源文件,将其编译成模块all: make -C $(KERN_DIR) M=`pwd` modules #编译.PHONY: clean #将clean声明为伪目标clean: make -C $(KERN_DIR) M=`pwd` modules clean #清除编译生成的文件4.进入到test目录下,打开终端,并将编译器添加进环境变量,可以生成模块test.ko文件:
环境变量指定为对应的编译器,这里以PD_X2600E_VAST_V2.0 为例
test@test:~/source/source_darwin_linux_x2000$ cat build/configs/x2600e_vast_v20_nand_5.10_factory_defconfig | grep toolchain_dirAPP_toolchain_dir=../tools/toolchains/mips-gcc930-glibc228 //获取编译器路径APP_uboot_toolchain_dir=../tools/toolchains/mips-gcc720-glibc229 cd testexport PATH=/home/your_linux/tools/toolchains/mips-gcc930-glibc228/bin:$PATH #配置环境变量,注意这里使用的是获取到的编译器绝对路径make kenny@kenny-computer:~/source/source_darwin_ilock$ cd test/kenny@kenny-computer:~/source/source_darwin_ilock/test$ export PATH=/home/kenny/source/source_darwin_ilock/tools/toolchains/mips-gcc930-glibc228/bin:$PATHkenny@kenny-computer:~/source/source_darwin_ilock/test$ make make -C /home/kenny/source/source_darwin_ilock/kernel/kernel M=`pwd` modules #编译make[1]: Entering directory '/home/kenny/source/source_darwin_ilock/kernel/kernel' Building modules, stage 2. MODPOST 1 modulesmake[1]: Leaving directory '/home/kenny/source/source_darwin_ilock/kernel/kernel'kenny@kenny-computer:~/source/source_darwin_ilock/test$ ls Makefile modules.order Module.symvers test.c test.ko test.mod.c test.mod.o test.o5.将生成的test.ko推入板子观察效果:
bhu@bhu-PC:~/Desktop/test$ adb push test.ko /usr/datatest.ko: 1 file pushed. 1.0 MB/s (2904 bytes in 0.003s)bhu@bhu-PC:~/Desktop/test$ adb shell# cd /usr/data/# lsevol test.komacaddr.txt wpa_supplicant.conf6.将模块手动加载进开发板,并用dmesg查看加载模块时的打印:
# insmod test.ko # lsmodModule Size Used by Tainted: G test 658 0 //test模块加载进去# dmesg | tail[ 7953.488834] <<-GTP-DEBUG->> [819]pre_touch:01, finger:81.[ 7953.488841] <<-GTP-DEBUG->> [950] (0)(19, 372)[24][ 7953.488848] <<-GTP-DEBUG->> [472]ID:0, X:19, Y:372, W:24[ 7953.499191] <<-GTP-DEBUG->> [819]pre_touch:01, finger:80.[ 7953.499199] <<-GTP-DEBUG->> [961]@@@@ touch UP ,pre_pen is 0,pre_finger is 1![ 7953.499204] <<-GTP-DEBUG->> [970]Touch Release![ 7953.509936] <<-GTP-DEBUG->> [819]pre_touch:00, finger:80.[ 7953.522234] <<-GTP-DEBUG->> [819]pre_touch:00, finger:80.[91048.779043] adbd (1066): /proc/1066/oom_adj is deprecated, please use /proc/1066/oom_score_adj instead.[91098.519476] test init //test模块加载打印7.将模块卸载,并查看卸载打印:
# rmmod test.ko# dmesg | tail[ 7953.488841] <<-GTP-DEBUG->> [950] (0)(19, 372)[24][ 7953.488848] <<-GTP-DEBUG->> [472]ID:0, X:19, Y:372, W:24[ 7953.499191] <<-GTP-DEBUG->> [819]pre_touch:01, finger:80.[ 7953.499199] <<-GTP-DEBUG->> [961]@@@@ touch UP ,pre_pen is 0,pre_finger is 1![ 7953.499204] <<-GTP-DEBUG->> [970]Touch Release![ 7953.509936] <<-GTP-DEBUG->> [819]pre_touch:00, finger:80.[ 7953.522234] <<-GTP-DEBUG->> [819]pre_touch:00, finger:80.[91048.779043] adbd (1066): /proc/1066/oom_adj is deprecated, please use /proc/1066/oom_score_adj instead.[91098.519476] test init[91405.937885] test exit //test模块卸载打印