内核编程实例,多文件的Makefile
内核编程实例,多文件的Makefile
经典的hello word测试
- ////# cat hello.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- static int __init hl_init( void )
- {
- printk("Hello,World! init\n");
- return 0;
- }
- static void __exit hl_cleanup( void )
- {
- printk("Goodbye, World! cleanup\n");
- }
- module_init(hl_init);
- module_exit(hl_cleanup);
- MODULE_LICENSE("GPL");
经典的由单个c文件产生模块的Makefile。
- # cat Makefile
- obj-m += hello.o
- CURRENT_PATH := $(shell pwd) #模块所在的当前路径
- LINUX_KERNEL := $(shell uname -r) #Linux内核源代码的当前版本
- LINUX_KERNEL_PATH := /usr/src/kernels/$(LINUX_KERNEL) #Linux内核源代码的绝对路径
- all:
- make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules #编译模块了
- clean:
- make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean #清理
编译
Make
就产生了hello.ko。安装
insmod hello.ko
卸载
rmmod hello
查看log
dmesg
- ................
- [12238.051159] Hello,World! init
- [12242.458122] Goodbye, World! cleanup
[]中的是时间戳。
多文件
我升级下,两个文件,hello.c和timer.c ,就是每隔一秒输出点东西来,开始输出hello init, 退出时输出exit。
hello.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- extern void timer_exit(void);
- extern int timer_init(void);
- static int __init hl_init( void )
- {
- printk("Hello,World! init\n");
- timer_init();
- return 0;
- }
- static void __exit hl_cleanup( void )
- {
- timer_exit();
- printk("Goodbye, World! cleanup\n");
- }
- module_init(hl_init);
- module_exit(hl_cleanup);
- MODULE_LICENSE("GPL");
timer.c
- #include <linux/timer.h>
- static struct timer_list my_timer;
- //定时函数
- void tm_say(unsigned long arg){
- printk( "timer do >>>>>>\n");
- mod_timer(&my_timer,jiffies+HZ);
- }
- //初始化模块和定时器
- int timer_init(void)
- {
- init_timer(&my_timer);
- my_timer.data=0;
- my_timer.function =tm_say;
- my_timer.expires = jiffies+HZ;
- //定时一秒钟
- add_timer(&my_timer);
- printk(KERN_EMERG "timer_k module inserted\n");
- return 0;
- }
- void timer_exit(void)
- {
- del_timer(&my_timer);
- printk("timer_k module exited\n");
- }
Makefile
- obj-m := hhh.o
- hhh-objs := hello.o timer.o
- KERNELBUILD := /lib/modules/`uname -r`/build
- default:
- echo " BUILD kmod"
- make -C $(KERNELBUILD) M=$(shell pwd) modules
- clean:
- make -C $(KERNELBUILD) M=$(shell pwd) clean
关键就是,target_name后面的"-objs"的指引。
编吧,make , insmod hhh.ko 等下 再 rmmod hhh 看看 dmes
- [16324.230095] Hello,World! init
- [16324.230095] timer_k module inserted
- [16325.232644] timer do >>>>>>
- [16326.237437] timer do >>>>>>
- [16327.244518] timer do >>>>>>
- [16328.247633] timer do >>>>>>
- [16329.248125] timer do >>>>>>
- [16329.864092] timer_k module exited
- [16329.864092] Goodbye, World! cleanup
经典的hello word测试
- ////# cat hello.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- static int __init hl_init( void )
- {
- printk("Hello,World! init\n");
- return 0;
- }
- static void __exit hl_cleanup( void )
- {
- printk("Goodbye, World! cleanup\n");
- }
- module_init(hl_init);
- module_exit(hl_cleanup);
- MODULE_LICENSE("GPL");
经典的由单个c文件产生模块的Makefile。
- # cat Makefile
- obj-m += hello.o
- CURRENT_PATH := $(shell pwd) #模块所在的当前路径
- LINUX_KERNEL := $(shell uname -r) #Linux内核源代码的当前版本
- LINUX_KERNEL_PATH := /usr/src/kernels/$(LINUX_KERNEL) #Linux内核源代码的绝对路径
- all:
- make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules #编译模块了
- clean:
- make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean #清理
编译
Make
就产生了hello.ko。安装
insmod hello.ko
卸载
rmmod hello
查看log
dmesg
- ................
- [12238.051159] Hello,World! init
- [12242.458122] Goodbye, World! cleanup
[]中的是时间戳。
多文件
我升级下,两个文件,hello.c和timer.c ,就是每隔一秒输出点东西来,开始输出hello init, 退出时输出exit。
hello.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- extern void timer_exit(void);
- extern int timer_init(void);
- static int __init hl_init( void )
- {
- printk("Hello,World! init\n");
- timer_init();
- return 0;
- }
- static void __exit hl_cleanup( void )
- {
- timer_exit();
- printk("Goodbye, World! cleanup\n");
- }
- module_init(hl_init);
- module_exit(hl_cleanup);
- MODULE_LICENSE("GPL");
timer.c
- #include <linux/timer.h>
- static struct timer_list my_timer;
- //定时函数
- void tm_say(unsigned long arg){
- printk( "timer do >>>>>>\n");
- mod_timer(&my_timer,jiffies+HZ);
- }
- //初始化模块和定时器
- int timer_init(void)
- {
- init_timer(&my_timer);
- my_timer.data=0;
- my_timer.function =tm_say;
- my_timer.expires = jiffies+HZ;
- //定时一秒钟
- add_timer(&my_timer);
- printk(KERN_EMERG "timer_k module inserted\n");
- return 0;
- }
- void timer_exit(void)
- {
- del_timer(&my_timer);
- printk("timer_k module exited\n");
- }
Makefile
- obj-m := hhh.o
- hhh-objs := hello.o timer.o
- KERNELBUILD := /lib/modules/`uname -r`/build
- default:
- echo " BUILD kmod"
- make -C $(KERNELBUILD) M=$(shell pwd) modules
- clean:
- make -C $(KERNELBUILD) M=$(shell pwd) clean
关键就是,target_name后面的"-objs"的指引。
编吧,make , insmod hhh.ko 等下 再 rmmod hhh 看看 dmes
[16324.230095] Hello,World! init
- [16324.230095] timer_k module inserted
- [16325.232644] timer do >>>>>>
- [16326.237437] timer do >>>>>>
- [16327.244518] timer do >>>>>>
- [16328.247633] timer do >>>>>>
- [16329.248125] timer do >>>>>>
- [16329.864092] timer_k module exited
- [16329.864092] Goodbye, World! cleanup
内核编程实例,多文件的Makefile的更多相关文章
- 《天书夜读:从汇编语言到windows内核编程》八 文件操作与注册表操作
1)Windows运用程序的文件与注册表操作进入R0层之后,都有对应的内核函数实现.在windows内核中,无论打开的是文件.注册表或者设备,都需要使用InitializeObjectAttribut ...
- HDFS简单编程实例:文件合并
下图显示了HDFS文件系统中路径为“localhost:50070/explorer.html#/user/hadoop”的目录中所有的文件信息: 对于该目录下的所有文件,我们将执行以下操作: 首先, ...
- Win64 驱动内核编程-29.强制解锁文件
强制解锁文件 强制解锁因其他进程占用而无法删除的文件. 1.调用 ZwQuerySystemInformation 的 16 功能号来枚举系统里的句柄 2.打开拥有此句柄的进程并把此句柄复制到自己的进 ...
- linux内核模块编程实例
linux内核模块编程实例 学号:201400814125 班级:计科141 姓名:刘建伟 1.确定本机虚拟机中的Ubuntu下Linux的版本 通过使用命令uname -a/uname -r/una ...
- 初探linux内核编程,参数传递以及模块间函数调用
一.前言 我们一起从3个小例子来体验一下linux内核编程.如下: 1.内核编程之hello world 2.模块参数传递 3.模块间 ...
- 如何搭建Visual Studio的内核编程开发环境
最近正在看<寒江独钓——Windows内核安全编程>这本书,感觉这本书非常好,有兴趣的朋友可以买来看看,有关这本书的信息请参考:http://www.china-pub.com/19559 ...
- Linux内核编程规范与代码风格
source: https://www.kernel.org/doc/html/latest/process/coding-style.html translated by trav, travmym ...
- Android 开发手记一NDK编程实例
在Android上,应用程序的开发,大部分基于Java语言来实现.要使用c或是c++的程序或库,就需要使用NDK来实现.NDK是Native Development Kit的简称.它是一个工具集,集成 ...
- 内核源码之Kconfig和Makefile
转自:http://www.cnblogs.com/image-eye/archive/2011/08/28/2156005.html 内核源码之Kconfig和Makefile Linux内核源码树 ...
随机推荐
- phantomjs环境搭建已经运行
1.下载phantomjs http://phantomjs.org/ 2.运行 新建phantomjs.bat,记得改目录路径 里面内容为: D:\java\phantomjs\phantomjs. ...
- 采用openFileOutput获取输出流
package com.example.login.service; import java.io.BufferedReader; import java.io.File; import java.i ...
- eclipse No Default Proposals 无提示
链接地址:http://blog.csdn.net/rogerjava/article/details/5689785 今天特抑郁,早上开机后发现eclipse的代码提示功能不好使了,Alt+/ 这么 ...
- MySQL优化必须调整的10项配置
当我们被人雇来监测MySQL性能时,人们希望我们能够检视一下MySQL配置然后给出一些提高建议.许多人在事后都非常惊讶,因为我们建议他们仅仅改动几个设置,即使是这里有好几百个配置项.这篇文章的目的在于 ...
- 三、IF...ELSE和缩进
IF...ELSE和缩进 根据用户输入的不同做不同的事情 注意语法结尾的冒号. 例1: name = input("Please input your name:") if nam ...
- 高级UIKit-02(文件操作)
[day3_1_Sandbox]:沙箱的介绍 snadbox沙箱沙盒 沙箱根目录下的几个文件夹: 1.应用名称.app存放应用程序的素材 2.Documents:存放应用运行时需要用到的数据(关键性数 ...
- 6.6.2 自己主动泛型化(automatic generalization)
6.6.2 自己主动泛型化(automatic generalization) 在这一章,我们已经实现了几个 F# 的高阶函数.也看到了在 F# 和 C# 中并排的实现.F# 实现的非常重要方面,是我 ...
- linux ftp批量上传和下载文件
一.登录ftp 输入 ftp 192.168.1.111 输入用户名:ftpuser 输入密码:aaa123 二.转到目标目录 输入:cd test ----test为文件夹 三.批量上传 输 ...
- chrome你这是入侵OSX了么?
今天突然发现安装chrome的应用后,会在Dock中出现如下图标,点击即可启动chrome相关应用,很是方便.
- form里两个submit按钮,在onsubmit中判断哪个被点
记下别人的解决方法(有效): 方法1:(已验证过) <form name="form1" onsubmit="show()"> ..... < ...