Linux 内核源码外编译 linux模块--编译驱动模块的基本方法
1、先编写一个简单的hello模块,hello.c 源码如下:
#ifndef __KERNEL__
# define __KERNEL__
#endif
#ifndef MODULE
# define MODULE
#endif
// 下面的是主要的内容
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
static int year=2012;
int hello_init()
{
printk(KERN_WARNING "Hello kernel, it's %d!\n",year);
return 0;
}
void hello_exit()
{
printk("Bye, kernel!\n");
}
// 下面两个为关键的模块函数
module_init(hello_init);
module_exit(hello_exit);
如果上面的代码看起来不太熟悉,那么需要查看以下相关的书籍,比如《Linux设备驱动程序,第三版》,也就是大名鼎鼎的LDD;
2、老式驱动模块编译方法:
直接写出make规则到makefile文件中,引用内核体系的头文件路径,举例如下:
# The path of kernel source code
INCLUDEDIR = /media/GoldenResources/linux/linux-2