1. 内核模块实例(book.c)
/*
* book.c - Demonstrates module documentation.
*/
# include <linux/module.h> /* Need by all modules */
# include <linux/kernel.h> /* Need for HERN_INFO */
# include <linux/init.h> /* Need for the macros */
# define DRIVER_AUTHOR "Star <s@dirac.org>"
# define DRIVER_DESC "A sample driver"
static char* bookName = "Good Book.";
static int bookNumber = 100;
static int __init book_init(void)
{
printk(KERN_INFO "Book name is %s\n", bookName);
printk(KERN_INFO "Book number is %d\n", bookNumber);
return 0;
}
static void __exit book_exit(void)
{
printk(KERN_INFO "Book module exit.\n");
}
module_init(book_init);
module_exit(book_exit);
module_param(bookName, charp, S_IRUGO);
module_param(bookNumber, int, S_IRUGO);
/*
You can use strings, like this:
*/
/*
Get rid of taint message by declaring code as GPL.
*/
MODULE_LICENSE("GPL");
/*
Or with defines, like this:
*/
MODULE_AUTHOR(DRIVER_AUTHOR); /* Who wrote this module? */
MODULE_DESCRIPTION(DRIVER_DESC); /* What does this module do */
2.代码解析
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR(DRIVER_AUTHOR);MODULE_DESCRIPTION(DRIVER_DESC);
3.Makefile文件的写法
Makefile 文件由五部分组成:显示规则 含规则 变量定义 makefile 指示符和注释
一条 Make 的规则原型为:
目标 ... :依赖 ..
命令代码:参照2.1
相关链接:内核模块编程入门:http://www.tldp.org/LDP/lkmpg/2.6/html/index.html