先抄了《Linux編程白皮書(shū)》上的代碼,貌似不成功;google后改了下,編譯成功。
hello.c
Makefile:
obj-m := hello.o
KERNELBUILD := /lib/modules/`uname -r`/build
default:
make -C $(KERNELBUILD) M=$(shell pwd) modules
然后
make
sudo insmod hello.ko // 載入模塊
dmesg // 即可看到Hello, world
sudo rmmod hello // 移除模塊
dmesg // 看到移除時(shí)信息
hello.c
#include <linux/kernel.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
int init_module()
{
printk("Hello, world - this is the kernel speaking\n");
return 0;
}
void cleanup_module()
{
printk("Short is the life of a kernel module\n");
}
#include <linux/module.h>
MODULE_LICENSE("GPL");
int init_module()
{
printk("Hello, world - this is the kernel speaking\n");
return 0;
}
void cleanup_module()
{
printk("Short is the life of a kernel module\n");
}
Makefile:
obj-m := hello.o
KERNELBUILD := /lib/modules/`uname -r`/build
default:
make -C $(KERNELBUILD) M=$(shell pwd) modules
然后
make
sudo insmod hello.ko // 載入模塊
dmesg // 即可看到Hello, world
sudo rmmod hello // 移除模塊
dmesg // 看到移除時(shí)信息