模块源码:
// 下面的是主要的内容
#include
#include
#include
MODULE_LICENSE("GPL");
static int year=2017;
int hello_init()
{
printk(KERN_WARNING "Hello kernel,it's %d!\n",year);
return 0;
}
void hello_exit()
{
printk("Bye,kernel!\n");
}
// 下面两个为关键的模块函数
module_init(hello_init);
module_exit(hello_exit);
Makefile:
ifneq ($(KERNELRELEASE),)
obj-m := helloworld.o
else
KDIR=/lib/modules/3.5.0-23-generic/build
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.mod.c *.symvers
endif
执行makefile编译模块-》执行insmod helloworld.ko控制台不会显示printk打印的消息,应该使用dmesg -c指令。成功看到打印的消息
root@ubuntu:/home/gec/driver# dmesg -c
[706893.639845] Hello kernel,it's 2017!
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
小编个人微信号 jb51ccc
喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!