1、
目标板为手机,有现成的pac包可以正常的运行
开发环境为linux,在linux源码下添加test目录, hello.c Makefile
2、hello.c内容
#include<linux/init.h>
#include <linux/module.h>
staticint __init hello_init(void)
{
rintk( "Helloword\n");
return 0;
}
staticvoid __exit hello_exit(void)
{
printk( "Goodbye, wordn");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("DualBSD/GPL");
3、Makefile内容
ifneq($(KERNELRELEASE),)
obj-m += hello.o
hello-objs += hello.o
hello -objs += xx.o
else
KDIR := /home6/night/marlin2/kernel/
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers
endif
Makefile解析:在test目录执行make的时候,KERNELRELEASE是没有定义的,所以就走else路线,KDIR定义了linux src的起始路径,里面包含了Makefile,执行第一个命令all, make –C 指定调用KDIR的makefile,M=$(PWD) 表明然后返回到当前目录继续读入、执行当前的Makefile。当从内核源码目录返回时,KERNELRELEASE已被定义,kbuild也被启动去解析kbuild语法的语句,make将继续读取else之前的内容,生成hello.ko目标文件,并编译其依赖文件hello.cxx.c. “M=”选项的作用是,当用户需要以某个内核为基础编译一个外部模块的话,需要在makemodules 命令中加入“M=dir”,程序会自动到你所指定的dir目录中查找模块源码,将其编译,生成KO文件。
4、在test目录执行make命令
得到错误
4.1 include/linux/autoconf.h orinclude/config/auto.conf are missing. Run 'make oldconfig && makeprepare' on kernel src to fix it.
à这主要是因为内核没有被编译过,或者编译之后相关的一些配置文件被删除,查看src目录是否有.config配置文件,出现这种现象一般没有,可直接cparch/arm/configs/xxx_defconfig ./.config,然后执行make prepare
4.2 生成ko之后,将其push到手机,adb push/system/lib/modules/ insmod hello.ko出现
insmod: init_module'hello.ko' failed (Exec format error),暂时未能解决