Linux PC端驱动实例

Linux PC端驱动程序实例

1新建驱动程序

新建程序文件hello.c

(1)hello.c的路径如下:

helloworld/hello.c

(2)hello.c的内容如下:

 1 #include <linux/init.h>
 2 #include <linux/module.h>
 3 
 4 static int __init hello_init(void)
 5 {
 6     printk("%s\n", __FUNCTION__);
 7     return 0;
 8 }
 9 
10 static void __exit hello_exit(void)
11 {
12     printk("%s\n", __FUNCTION__);
13 }
14 
15 module_init(hello_init);
16 module_exit(hello_exit);
View Code

 

2添加配置文件

新建配置文件Makefile

(1)Makefile的路径如下:

Helloworld/Makefile

(2)Makefile的内容如下:

1 obj-m := hello.o  #这个是要中间文件
2 Kernel_path=/usr/src/linux-headers-$(shell uname -r)  #内核存在的路径
3 all:
4     make -C $(Kernel_path) M=$(PWD) modules
5 clean:
6     make -C $(Kernel_path) M=$(PWD) clean
View Code

 

3编译

进入helloworld目录,并执行make命令,生成hello.ko模块文件

 

4加载/卸载模块

4.1加载模块

(1)执行以下命令加载模块

# insmod hello.ko

(2)用dmesg查看信息

# hello_init

 

4.2卸载模块

(1)执行以下命令卸载模块

# rmmod hello.ko

(2)用dmesg查看信息

# hello_exit

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值