zoukankan      html  css  js  c++  java
  • [国嵌攻略][104][Linux内核模块设计]

    内核模块示例

    #inlcude <linux/init.h>
    #inlcude <linux/module.h>
    
    static int hello_init(){
        printk(KERN_WARNING”hello_init
    ”);
        return 0;
    }
    
    static void hello_exit(){
        printk(KERN_INFO”hello_exit
    ”);
    }
    
    module_init(hello_init);
    module_exit(hello_exit);

    特点:

    内核模块代码没有main函数,内核模块的入口由module_init()指明。当使用insmod命令加载内核模块时,宏module_init()所指明的函数hello_init会得到调用。当使用rmmod命令卸载内核模块时,宏module_exit()所指明的函数hello_exit会被调用。内核模块包含<linux/init.h>和<linux/module.h>两个头文件。

    编译内核模块

    obj-m := hello.o

    hello-objs = file1.o file2.o file3.o

    KDIR := .../linux_kernel

    all:

        make –C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-

    clean:

        rm –f *.o *.ko *.order *.symvers

    说明:

    1.目标文件

    obj-m表示要生成的模块名,如果有多个文件生成一个模块,需要加上xxx-objs来指明依赖的文件。

    2.内核路径

    KDIR用来指明生成生成的内核模块依赖的内核代码。

    3.编译规则

    –C $(KDIR)表示进入依赖的内核文件,M=$(PWD)表示要编译的内核模块,modules ARCH=arm CROSS_COMPILE=arm-linux-表要编译的内核模块使用的处理器架构和交叉工具链。

    hello.c

    #include <linux/init.h>
    #include <linux/module.h>
    
    //加载函数
    static int hello_init(){
        printk(KERN_WARNING"hello_init
    ");
        
        return 0;
    }
    
    //卸载函数
    static void hello_exit(){
        printk(KERN_WARNING"hello_exit
    ");
    }
    
    module_init(hello_init);
    module_exit(hello_exit);

    Makefile

    obj-m := hello.o
    
    KDIR := /space/work/guoqian/liunxkernel/000/kernel/linux-mini2440
    
    all :
        make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
        
    clean :
        @rm -f *.o *.ko *.mod.* *.order *.symvers
  • 相关阅读:
    面向对象的设计原则
    在VC中Debug下是运行结果正确的,但是在Release下却有错,总算找到原因
    聚合和组合
    痛苦呀,代码
    MSDN和VS98
    阅读代码的难度
    好香,方便面
    人的重要性
    FIT For .NET(1)
    ASP.NET Microsoft .NET Pet Shop 3.x(二)
  • 原文地址:https://www.cnblogs.com/d442130165/p/5245130.html
Copyright © 2011-2022 走看看