本文共 865 字,大约阅读时间需要 2 分钟。
我们需要将一个简单的内核模块开发出来,模块名称为hello_world。以下是实现步骤:
代码示例如下:
#include#include #include MODULE_LICENSE("GPL");static int __init hello_init(void) { printk("hello init\n"); return 0;}static void __exit hello_exit(void) { printk("hello exit\n");}module_init(hello_init);module_exit(hello_exit);
这部分将介绍如何使用Makefile来编译内核模块。我们将使用Kconfig机制来进行配置。
make -C /home/minicoco/Dev/Dev/nanopi/kernel/linux-3.4.y M=`pwd`
hello: make -C ${KERNEL_DIR} M=`pwd` hello_world.oclean: make -C ${KERNEL_DIR} M=`pwd` cleanobj-m += hello_world.o 这样模块将会在当前目录输出。
在完成编译后,我们需要测试模块的加载与卸载过程。
insmod hello_world.ko
dmesg | tail
rmmod hello_world
在卸载后,再次查看日志以确认模块是否成功卸载。
转载地址:http://ypzhz.baihongyu.com/