最近项目须要使用Linux系统开发,借此机会学习一下Linux驱动开发linux
hello word代码hello.cshell
#include <linux/module.h> #include <linux/init.h> static int hello_init(void)//模块入口 { printk("Hello, I'm ready!\n"); return 0; } static void hello_exit(void)//模块出口 { printk("I'll be leaving, bye!\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL");
mekefile文件学习
# If KERNELRELEASE is defined, we've been invoked from the # kernel build system and can use its language. ifneq ($(KERNELRELEASE),) obj-m := hello.o # Otherwise we were called directly from the command # line; invoke the kernel build system. else KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules endif
在hello.c目录下,shell终端中执行make命令,生成hello.ko文件;ui
使用如下命令插入spa
sudo insmod hello.ko
lsmode//查看模块
sudo rmmod hello.ko
问题:插入模块后本应打印出字符,后发现是printk打印级别问题,查看/var/log/syslog下发现有打印出字符.net
使用一下代码;code
printk(KERN_EMERG "EMERG\n"); printk(KERN_ALERT "ALERT\n"); printk(KERN_CRIT " CRIT\n"); printk(KERN_ERR " ERR\n"); printk(KERN_WARNING ""WARNING\n"); printk(KERN_NOTICE "NOTICE\n"); printk(KERN_INFO "INFO\n"); printk(KERN_DEBUG "DEBUG\n");
发现终端仍是没有输出,后查到https://blog.csdn.net/xj626852095/article/details/9746547下说是Ubuntu的问题,blog
只用使用#dmesg自行查看了开发