linux模块

主要内容:
  1、模块程序
  2、gcc编译
  3、交叉编译
  4、模块安装与卸载   参考:http:
//blog.csdn.net/hanchaoman/article/details/6962293
     《Linux设备驱动程序》
1、hello.c #include <linux/init.h> #include <linux/module.h> /* 告诉内核,该模块带有一个自由许可证 */ MODULE_LICENSE("Duall BSD/GPL"); static int __init hello_init(void) { printk(KERN_ALERT "hello, world\n"); return 0; } static void __exit hello_exit(void) { printk(KERN_ALERT "goodbye, cruel world\n"); } /* 模块初始化和退出函数 */ module_init(hello_init); module_exit(hello_exit);
2、常规编译Makefile # 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/`uname -r`/build PWD := $(shell pwd) #使用-C选项切换到内核目录 #M=选项使 makefile 在试图建立模块目标前,回到你的模块源码目录 all: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules clean: rm -f *.ko *.o *.mod.o *.mod.c *.order *.symvers endif
这个 makefile 在一次典型的建立中要被读
2 次. 当从命令行中调用这个 makefile , 它注意到 KERNELRELEASE 变量没有设置.它利用这样一个事实来定位内核源码目录, 即已安装模块目录中的符号连接指回内 核建立树. 如果你实际上没有运行你在为其而建立的内核, 你可以在命令行提供一个KERNELDIR= 选项, 设置 KERNELDIR 环境变量, 或者重写 makefile 中设置 KERNELDIR 的 那一行. 一旦发现内核源码树, makefile 调用 all: 目标, 来运行第 2 个 make 命令( 在 makefile 里参数化成 $(MAKE))象前面>描述过的一样来调用内核建立系统. 在第 2 次读, makefile 设置 obj-m, 并且内核的 makefile 文件完成实际的建立模块工作. 3、交叉编译Makefile ifneq ($(KERNELRELEASE),) obj-m := hello.o else #内核源码树的路径 KERNELDIR := /home/tony/work/iproc/kernel/linux-3.6.5 PWD := $(shell pwd) #指定交叉编译工具的路径 CROSS_COMPILE := /home/tony/work/iproc/buildroot/host/usr/bin/arm-linux- #架构 ARCH = arm all: make -C $(KERNELDIR) M=$(PWD) modules ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) clean: rm -f *.ko *.o *.mod.o *.mod.c *.order *.symvers endif #编译完成之后就,将hello.ko下载到开发板,就可以安装
4、
4.1 安装模块   sudo insmod hello.ko 查看输出信息   dmesg | tail -5 [ 101.312168] __ratelimit: 18 callbacks suppressed [ 101.313230] type=1503 audit(1445259881.131:18): operation="capable" pid=1665 parent=1660 profile="/usr/sbin/cupsd" name="sys_admin" [ 3009.787926] hello: module license 'Duall BSD/GPL' taints kernel. [ 3009.788507] Disabling lock debugging due to kernel taint [ 3009.810441] hello, world 4.2 卸载模块   sudo rmmod hello.ko   dmesg | tail -5 [ 101.313230] type=1503 audit(1445259881.131:18): operation="capable" pid=1665 parent=1660 profile="/usr/sbin/cupsd" name="sys_admin" [ 3009.787926] hello: module license 'Duall BSD/GPL' taints kernel. [ 3009.788507] Disabling lock debugging due to kernel taint [ 3009.810441] hello, world [ 3052.781876] goodbye, cruel world
 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值