首先引用网上最常见的Helloworld模块:
Helloworld.c
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
static int hello_init(void)
{
printk("Hello world.\n");
return 0;
}
static void hello_exit(void)
{
printk("Goodbye world.\n");
return;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("John");
MODULE_LICENSE()指明认证方式,现在支持的有:“GPL” “GPL v2" "GPL and additional rights" "Dual BSD/GPL" "Dual MIT/GPL" "Dual MPL/GPL" ,module_init()指明模块的入口,这是必需的;module_exit()指明模块的出口,这也是必需的。
编写自己的Makefile:
obj-m:=helloworld.o
KDIR:=/lib/modules/2.6.31-14-generic/build
MAKE:=make
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
helloworld.o为目标文件产生的.o文件,KDIR指明了内核的位置,这需要根据具体情况修改该文件。
接下来,make命令,就可以进行编译了,这是可以产生我们需要的.ko文件。
在终端下进入adb,执行以下命令来加载模块
adb push ~/helloworld.ko /system/vendor/modules/
adb shell
cd /system/vendor/modules/
insmod helloworld.ko
这是模块被加载到平板中,卸载使用rmmod命令,打印信息使用dmesg。
注:根据不同平台,加载方法多用化,android设备使用adb比较方便,ARM直接可以使用SecureCRT的终端比较方便。