linux下hello—world驱动,做成功了,现在总结一下

    技术2022-05-19  23

    一。这是源码hello_dri.c

    #include <linux/init.h> /*包含模块注册和注销宏*/#include <linux/module.h> /*驱动编程必须包含*/#include <linux/kernel.h>

     

    static int hello_init(void){printk( "<1>Hello World 1./n" );

    return 0;}

    static void hello_exit(void){printk( KERN_ALERT "Goodbye 1./n" );}

    module_init(hello_init);module_exit(hello_exit);

    MODULE_LICENSE("Dual BSD/GPL");// end of source

    二。这是Makefile

    obj-m:=hello_dri.o #目标模块的名字 

    KDIR:=/lib/modules/$(shell uname -r)/build #内核源码支持

    PWD:=$(shell pwd) #自己程序所在目录

     

    default: #输入make ,默认使用这里

    $(MAKE) -C $(KDIR) M=$(PWD) modules

    clean:symver

    $(MAKE) -C $(KDIR) M=$(PWD) clean

    rm -rf Module.markers modules.order Module.symvers

    end of Makefile

    注意

    1.程序名字是有讲究的

    2.源码和Makefile放在一个目录

    三。编译

    $make

    在当前目录生成了。ko驱动文件

    四。运行

    进入字符终端(ctrl+alt+f1),$sudo insmod hello_dri.ko

    显示printk里的内容

    $sudo rmmod hello_dri.ko

    注意:

    1.加载时是hello_dri.ko 卸载时是hello_dri

    2.在字符终端才会有显示。否则在图形界面的终端不会显示,但会在/var/log/messages里面会有显示

     

    重点:

    由于2.6核心的Makefile和别的版本的不一样,所以需要研究一下,贴出参考资料

    http://lwn.net/Articles/21817/ (纯英文,很专业,很好用)

    http://linux.chinaunix.net/techdoc/develop/2008/07/25/1020310.shtml (论坛上的)


    最新回复(0)