Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1137915
  • 博文数量: 241
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2279
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-27 19:53
个人简介

JustForFun

文章分类

全部博文(241)

文章存档

2023年(8)

2022年(2)

2021年(3)

2020年(30)

2019年(11)

2018年(27)

2017年(54)

2016年(83)

2015年(23)

我的朋友

分类: LINUX

2018-09-29 11:15:04

目的: 主要用于熟悉驱动程序编写及调试基本流程。
1.准备

         操作系统: ubuntu12.04 LTS
2.源码

    #include
    #include
     
    static int hello_init(void)
    {
        pr_info("Hello, World.\n");
        return 0;
    }
     
    static void hello_exit(void)
    {
        pr_info("Goodbye, cruel world\n");
    }
     
    module_init(hello_init);
    module_exit(hello_exit);
     
    MODULE_LICENSE("Dual BSD/GPL");

3.Makefile

    obj-m := hello.o
     
    KERNELDIR := /usr/src/linux-headers-$(shell uname -r)
    PWD := $(shell pwd)
     
    modules:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
    modules_install:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

4.编译
make:

    ckt@ubuntu:~/work/ldd/hello$ make
    make -C /usr/src/linux-headers-3.13.0-32-genericM=/home/ckt/work/ldd/hello modules
    make[1]: Entering directory`/usr/src/linux-headers-3.13.0-32-generic'
      CC [M]  /home/ckt/work/ldd/hello/hello.o
      Buildingmodules, stage 2.
      MODPOST 1modules
      LD [M]  /home/ckt/work/ldd/hello/hello.ko
    make[1]: Leaving directory`/usr/src/linux-headers-3.13.0-32-generic'

5.测试

             lsmod                  #查看当前加载的模块
             sudo insmod hello.ko         #加载hello.ko模块
             sudo rmmod hello             #移除hello.ko模块
             cat/var/log/syslog     #查看日志






图片

图片


 


阅读(1761) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~