内核编程实例,多文件的Makefile

经典的hello word测试

  1. ////# cat hello.c
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/init.h>
  5. static int __init hl_init( void )
  6. {
  7. printk("Hello,World! init\n");
  8. return 0;
  9. }
  10. static void __exit hl_cleanup( void )
  11. {
  12. printk("Goodbye, World! cleanup\n");
  13. }
  14. module_init(hl_init);
  15. module_exit(hl_cleanup);
  16. MODULE_LICENSE("GPL");

经典的由单个c文件产生模块的Makefile。

  1. # cat Makefile
  2. obj-m += hello.o
  3. CURRENT_PATH := $(shell pwd) #模块所在的当前路径
  4. LINUX_KERNEL := $(shell uname -r) #Linux内核源代码的当前版本
  5. LINUX_KERNEL_PATH := /usr/src/kernels/$(LINUX_KERNEL) #Linux内核源代码的绝对路径
  6. all:
  7. make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules #编译模块了
  8. clean:
  9. make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean #清理

编译

Make
就产生了hello.ko。

安装

insmod hello.ko

卸载

rmmod hello

查看log

dmesg

    1. ................
    2. [12238.051159] Hello,World! init
    3. [12242.458122] Goodbye, World! cleanup
[]中的是时间戳。

多文件
我升级下,两个文件,hello.c和timer.c ,就是每隔一秒输出点东西来,开始输出hello init, 退出时输出exit。
hello.c

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. extern void timer_exit(void);
  5. extern int timer_init(void);
  6. static int __init hl_init( void )
  7. {
  8. printk("Hello,World! init\n");
  9. timer_init();
  10. return 0;
  11. }
  12. static void __exit hl_cleanup( void )
  13. {
  14. timer_exit();
  15. printk("Goodbye, World! cleanup\n");
  16. }
  17. module_init(hl_init);
  18. module_exit(hl_cleanup);
  19. MODULE_LICENSE("GPL");

timer.c

  1. #include <linux/timer.h>
  2. static struct timer_list my_timer;
  3. //定时函数
  4. void tm_say(unsigned long arg){
  5. printk( "timer do >>>>>>\n");
  6. mod_timer(&my_timer,jiffies+HZ);
  7. }
  8. //初始化模块和定时器
  9. int timer_init(void)
  10. {
  11. init_timer(&my_timer);
  12. my_timer.data=0;
  13. my_timer.function =tm_say;
  14. my_timer.expires = jiffies+HZ;
  15. //定时一秒钟
  16. add_timer(&my_timer);
  17. printk(KERN_EMERG "timer_k module inserted\n");
  18. return 0;
  19. }
  20. void timer_exit(void)
  21. {
  22. del_timer(&my_timer);
  23. printk("timer_k module exited\n");
  24. }

Makefile

  1. obj-m := hhh.o
  2. hhh-objs := hello.o timer.o
  3. KERNELBUILD := /lib/modules/`uname -r`/build
  4. default:
  5. echo " BUILD kmod"
  6. make -C $(KERNELBUILD) M=$(shell pwd) modules
  7. clean:
  8. make -C $(KERNELBUILD) M=$(shell pwd) clean

关键就是,target_name后面的"-objs"的指引。
编吧,make , insmod hhh.ko 等下 再 rmmod hhh 看看 dmes

  1. [16324.230095] Hello,World! init
  2. [16324.230095] timer_k module inserted
  3. [16325.232644] timer do >>>>>>
  4. [16326.237437] timer do >>>>>>
  5. [16327.244518] timer do >>>>>>
  6. [16328.247633] timer do >>>>>>
  7. [16329.248125] timer do >>>>>>
  8. [16329.864092] timer_k module exited
  9. [16329.864092] Goodbye, World! cleanup

经典的hello word测试

  1. ////# cat hello.c
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/init.h>
  5. static int __init hl_init( void )
  6. {
  7. printk("Hello,World! init\n");
  8. return 0;
  9. }
  10. static void __exit hl_cleanup( void )
  11. {
  12. printk("Goodbye, World! cleanup\n");
  13. }
  14. module_init(hl_init);
  15. module_exit(hl_cleanup);
  16. MODULE_LICENSE("GPL");

经典的由单个c文件产生模块的Makefile。

  1. # cat Makefile
  2. obj-m += hello.o
  3. CURRENT_PATH := $(shell pwd) #模块所在的当前路径
  4. LINUX_KERNEL := $(shell uname -r) #Linux内核源代码的当前版本
  5. LINUX_KERNEL_PATH := /usr/src/kernels/$(LINUX_KERNEL) #Linux内核源代码的绝对路径
  6. all:
  7. make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules #编译模块了
  8. clean:
  9. make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean #清理

编译

Make
就产生了hello.ko。

安装

insmod hello.ko

卸载

rmmod hello

查看log

dmesg

    1. ................
    2. [12238.051159] Hello,World! init
    3. [12242.458122] Goodbye, World! cleanup
[]中的是时间戳。

多文件
我升级下,两个文件,hello.c和timer.c ,就是每隔一秒输出点东西来,开始输出hello init, 退出时输出exit。
hello.c

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. extern void timer_exit(void);
  5. extern int timer_init(void);
  6. static int __init hl_init( void )
  7. {
  8. printk("Hello,World! init\n");
  9. timer_init();
  10. return 0;
  11. }
  12. static void __exit hl_cleanup( void )
  13. {
  14. timer_exit();
  15. printk("Goodbye, World! cleanup\n");
  16. }
  17. module_init(hl_init);
  18. module_exit(hl_cleanup);
  19. MODULE_LICENSE("GPL");

timer.c

  1. #include <linux/timer.h>
  2. static struct timer_list my_timer;
  3. //定时函数
  4. void tm_say(unsigned long arg){
  5. printk( "timer do >>>>>>\n");
  6. mod_timer(&my_timer,jiffies+HZ);
  7. }
  8. //初始化模块和定时器
  9. int timer_init(void)
  10. {
  11. init_timer(&my_timer);
  12. my_timer.data=0;
  13. my_timer.function =tm_say;
  14. my_timer.expires = jiffies+HZ;
  15. //定时一秒钟
  16. add_timer(&my_timer);
  17. printk(KERN_EMERG "timer_k module inserted\n");
  18. return 0;
  19. }
  20. void timer_exit(void)
  21. {
  22. del_timer(&my_timer);
  23. printk("timer_k module exited\n");
  24. }

Makefile

  1. obj-m := hhh.o
  2. hhh-objs := hello.o timer.o
  3. KERNELBUILD := /lib/modules/`uname -r`/build
  4. default:
  5. echo " BUILD kmod"
  6. make -C $(KERNELBUILD) M=$(shell pwd) modules
  7. clean:
  8. make -C $(KERNELBUILD) M=$(shell pwd) clean

关键就是,target_name后面的"-objs"的指引。
编吧,make , insmod hhh.ko 等下 再 rmmod hhh 看看 dmes 
[16324.230095] Hello,World! init

  1. [16324.230095] timer_k module inserted
  2. [16325.232644] timer do >>>>>>
  3. [16326.237437] timer do >>>>>>
  4. [16327.244518] timer do >>>>>>
  5. [16328.247633] timer do >>>>>>
  6. [16329.248125] timer do >>>>>>
  7. [16329.864092] timer_k module exited
  8. [16329.864092] Goodbye, World! cleanup

内核编程实例,多文件的Makefile的更多相关文章

  1. 《天书夜读:从汇编语言到windows内核编程》八 文件操作与注册表操作

    1)Windows运用程序的文件与注册表操作进入R0层之后,都有对应的内核函数实现.在windows内核中,无论打开的是文件.注册表或者设备,都需要使用InitializeObjectAttribut ...

  2. HDFS简单编程实例:文件合并

    下图显示了HDFS文件系统中路径为“localhost:50070/explorer.html#/user/hadoop”的目录中所有的文件信息: 对于该目录下的所有文件,我们将执行以下操作: 首先, ...

  3. Win64 驱动内核编程-29.强制解锁文件

    强制解锁文件 强制解锁因其他进程占用而无法删除的文件. 1.调用 ZwQuerySystemInformation 的 16 功能号来枚举系统里的句柄 2.打开拥有此句柄的进程并把此句柄复制到自己的进 ...

  4. linux内核模块编程实例

    linux内核模块编程实例 学号:201400814125 班级:计科141 姓名:刘建伟 1.确定本机虚拟机中的Ubuntu下Linux的版本 通过使用命令uname -a/uname -r/una ...

  5. 初探linux内核编程,参数传递以及模块间函数调用

    一.前言                                  我们一起从3个小例子来体验一下linux内核编程.如下: 1.内核编程之hello world 2.模块参数传递 3.模块间 ...

  6. 如何搭建Visual Studio的内核编程开发环境

    最近正在看<寒江独钓——Windows内核安全编程>这本书,感觉这本书非常好,有兴趣的朋友可以买来看看,有关这本书的信息请参考:http://www.china-pub.com/19559 ...

  7. Linux内核编程规范与代码风格

    source: https://www.kernel.org/doc/html/latest/process/coding-style.html translated by trav, travmym ...

  8. Android 开发手记一NDK编程实例

    在Android上,应用程序的开发,大部分基于Java语言来实现.要使用c或是c++的程序或库,就需要使用NDK来实现.NDK是Native Development Kit的简称.它是一个工具集,集成 ...

  9. 内核源码之Kconfig和Makefile

    转自:http://www.cnblogs.com/image-eye/archive/2011/08/28/2156005.html 内核源码之Kconfig和Makefile Linux内核源码树 ...

随机推荐

  1. [Android] FileInputStream跟踪

    1. 源起 需要跟踪FileInputStream的Read的Nativie实现,开始走了弯路,Java工程下的FileInputStream实现与Android工程的实现不同.另外,http://b ...

  2. A Magic Lamp(贪心+链表)

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. AOP编程

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. [转]给Linux系统管理员准备的Nmap命令的29个实用范例+ tsysv 系统服务器管理器

    原文链接:http://os.51cto.com/art/201401/428152.htm Nmap即网络映射器对Linux系统/网络管理员来说是一个开源且非常通用的工具.Nmap用于在远程机器上探 ...

  5. java--线程状态

    1.新建状态 Thread t1 = new Thread(); 创建之后,就已经有了相应的内存和其他资源,但是还是处于不可运行状态. 2.就绪状态 当一个线程使用.start()启动之后就处于就绪状 ...

  6. SFTP上传下载(C#)

    sftp是ftp协议的升级版本,是牺牲上传速度为代价,换取安全性能,本人开始尝试使用Tamir.SharpSSH.dll但它对新版本的openssh 不支持,所有采用Ssh.Net方式 需要依赖:Re ...

  7. KVM虚拟机介绍

    一 KVM虚拟机简介 kernel-based Virtual Machine的简称,是一个开源的 系统虚拟化模块,自Linux 2.6.20之后集成在Linux的各个主要发行版本中.它使用Linux ...

  8. SQL Server截取字符串和处理中文技巧

    一 环境介绍 SQL  Server PRINT @@VERSION MicrosoftSQLServer2012-11.0.2100.60(X64) Feb10201219:39:15 Copyri ...

  9. c#基础练习之if结构

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace if语句 ...

  10. c语言,enum

    在设置错误代号时,使用enum比宏更好看. #include <stdio.h> typedef enum { retOK = 0, errComInterruption = 0x1000 ...