zoukankan      html  css  js  c++  java
  • 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 "); return 0; } static void __exit hello_exit(void) { printk(KERN_ALERT "goodbye, cruel world "); } /* 模块初始化和退出函数 */ 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
     
  • 相关阅读:
    redis安装及简单命令
    struts2 第二天
    初学struts2-入门案列
    hibernate第二天
    hibernate入门
    同义词,索引,表分区
    表空间,序列
    orcale函数
    orcale错题分析
    orcale开篇
  • 原文地址:https://www.cnblogs.com/helloworldtoyou/p/4893256.html
Copyright © 2011-2022 走看看