在Ubuntu上学驱动之HelloWorld(一)

开发环境: ubuntu 16.04

一、hello.c源码:

/*                                                     
 * $Id: hello.c,v 1.5 2004/10/26 03:32:21 corbet Exp $ 
 */                                                    
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
	printk(KERN_ALERT "Hello, world\n");
	return 0;
}

static void hello_exit(void)
{
	printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

三、hello.c分析:

1、内核需要自己单独打印输出函数,是因为它在运行时不能依赖c库。
2、模块能够调用printk是因为在insmod函数装入模块后,模块就链接到内核了。
3、module_init()与module_exit()宏定义, 声明一个模块的初始化和清理函数.

三、Makefile源码:

KERNELDIR ?= /lib/modules/`uname -r`/build

hello_world-objs := hello.o
obj-m := hello_world.o

all:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules;

clean:
        $(MAKE) -C $(KERNELDIR) SUBDIRS=$(PWD) clean;
        rm -f *.ko;
        
.PHONY: modules modules_install clean

四、Makefile分析:

1、先来分析第一句 :KERNELDIR ?= /lib/modules/'uname -r’/build

这句是对KERNELDIR进行赋值,这个变量是后面我们用到的指代内核源码目录用的。
uname -r 表示内核版本号(可以在命令行输入看看)
/lib/modules/'uname -r'/build 就是完整的内核库所在路径

2、分析: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules;

这句是Makefile的规则:这里的$(MAKE)就相当于make,-C 选项的作用是指将当前工作目录转移到你所指定的位置。“M=”选项的作用是,当用户需要以某个内核为基础编译一个外部模块的话,需要在make modules 命令中加入“M=dir”,程序会自动到你所指定的dir目录中查找模块源码,将其编译,生成KO文件。
3、分析: .PHONY: modules modules_install clean
这句话是的作用是保证modules,modules_install,clean这三个命令能正常完成。.PHONY 这是一个特殊目标名称,还有其它的,如:
.SUFFIXES,.DEFAULT,.PRECIOUS,.INTERMEDIATE,.SECONDARY,.SECONDEXPANSION,.DELETE_ON_ERROR,.IGNORE .LOW_RESOLUTION_TIME .SILENT .EXPORT_ALL_VARIABLES .NOTPARALLEL
它们的具体用法可以参考GNU手册中的Special Built-in Target Names章节。
.PHONY目标的具体意思是如果在Makefile的工作目录中有名如:modules,modules_install,clean等文件时命令会出错。它是防止这出错的方式。

五、编译:

# make
make -C /lib/modules/`uname -r`/build M=/home/yang/yfwfile/linux_driver/source_code/misc-modules/hello modules;
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-166-generic'
  CC [M]  /home/yang/yfwfile/linux_driver/source_code/misc-modules/hello/hello.o
  LD [M]  /home/yang/yfwfile/linux_driver/source_code/misc-modules/hello/hello_world.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/yang/yfwfile/linux_driver/source_code/misc-modules/hello/hello_world.mod.o
  LD [M]  /home/yang/yfwfile/linux_driver/source_code/misc-modules/hello/hello_world.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-166-generic'
# ls
hello.c  hellop.c        hello_world.mod.c  hello_world.o  modules.order
hello.o  hello_world.ko  hello_world.mod.o  Makefile       Module.symvers

六、安装模块

insmod、rmmod 安装与卸载驱动的命令
在这里插入图片描述
如果没有输出"Hello,world"可能是因为你在终端模拟器下运行的,因为在终端模拟器下时会把内核消息输出到日志文件/var/log/kern.log中。如果在字符终端运行的话就会直接打出Hello,world

使用 tail /var/log/kernel.log查看
在这里插入图片描述
在字符终端下(如下图):
在这里插入图片描述
能正常看到输出。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yfw&武

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值