Linux驅動學習1.hello world;

最近項目須要使用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自行查看了開發

相關文章
相關標籤/搜索