最近项目需要使用Linux系统开发,借此机会学习一下Linux驱动开发
hello word代码hello.c
#include <linux/module.h>
#include <linux/init.h>
static int hello_init(void)//模块入口
{
printk("Hello, I'm ready!\n");
return ;
}
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文件;
使用以下命令插入
sudo insmod hello.ko
lsmode//查看模块
sudo rmmod hello.ko
问题:插入模块后本应打印出字符,后发现是printk打印级别问题,查看/var/log/syslog下发现有打印出字符
使用一下代码;
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的问题,
只用使用#dmesg自行查看了