加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

简单的Linux驱动程序以及如何加载/卸载驱动

发布时间:2020-12-13 23:46:51 所属栏目:Linux 来源:网络整理
导读:今天记录一下简单的 Linux驱动程序怎么写以及如何加载 /卸载驱动 以 hello.c为例: hello.c ? #ifndef __KERNEL__# define __KERNEL__#endif#ifndef MODULE# define MODULE#endif#include linux/kernel.h#include linux/module.h#include linux/init.hMODULE

今天记录一下简单的Linux驱动程序怎么写以及如何加载/卸载驱动

hello.c为例:

hello.c

?

#ifndef __KERNEL__
#  define __KERNEL__
#endif
#ifndef MODULE
#  define MODULE
#endif

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void){
    printk(KERN_ALERT "HELLO WORLDn");
    return 0;
}
static void hello_exit(void){
    printk(KERN_ALERT "GOODBYE CRUEL WORLDn");
}
module_init(hello_init);
module_exit(hello_exit);

/*
3.5.0-23-generic
make -C /lib/modules/3.5.0-23-generic/build M=`pwd` modules
*/

  

Makefile来编译:

Makefile

obj-m := hello.o

all :
    $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

?

以下终端是编译命令,下图所示编译成功:

加载和移除驱动:

sudo insmod ./hello.ko
sudo rmmod hello       

  

查看日志

tail /var/log/kern.log 

参考博客https://www.cnblogs.com/QuLory/archive/2012/10/23/2736339.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读