代码:
/* hello.c */
#include <linux/module.h>
#include <linux/kernel.h> /* Optional, to include printk() prototype */
#include <linux/init.h> /* Optional, to include module_init() macros */
static int __init hello_init(void)
{
printk("Hello, how are you?/n");
/* A non 0 return means init_module failed; module can't be loaded. */
return 0;
}
static void __exit hello_exit(void)
{
printk("I'm fine. Goodbye!/n");
}
module_init(hello_init);
module_exit(hello_exit);
编译方法:
1. 准备好一个配置编译好的内核代码树
2. 把 hello.c 放置在一个单独的目录,并在这个目录中编写一个Makefile:
obj-m := hello.o
3. 编译命令:make -C <内核代码树目录> M=`pwd`
运行方法:
1. 当前运行的内核和要执行的模块是同一版本,root 权限。
2. insmod ./hello.ko
3. 卸载:rmmod hello
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。