比思論壇

標題: linux2.4与2.6下的模块编程对比 [打印本頁]

作者: perl0302    時間: 2014-11-15 20:44
標題: linux2.4与2.6下的模块编程对比
linux2.4与2.6下的模块编程对比
模块编程在2.4和2.6的kernel中的区别
以hello world为例:
2.4:
========================
hello.c
========================
/* How to compile:
* gcc -I /usr/src/linux-2.4/include -DMODULE -D__KERNEL__ -c hello.c
*/
#ifndef MODULE
#define MODULE
#endif
#ifndef __KERNEL__
#define __KERNEL__
#endif
#include
#include  
int init_module(void)
{
printk(KERN_ALERT "Hello World!\n");
return 0; // Must return 0!
}
void cleanup_module(void)
{
printk(KERN_ALERT "Goodbye world!\n");
}
MODULE_LICENSE("GPL"); // Avoid "no license" warning.
========================
Makefile
========================
CC=gcc
MODCFLAGS := -I /usr/src/linux-2.4/include -DMODULE -D__KERNEL__
hello.o:hello.c
$(CC) $(MODCFLAGS) -c hello.c
.PHONY:clean
clean:
rm -f hello.o
========================
# make
# insmod hello.o
# lsmod
# rmmod hello
注:2.4的模块编程中,MODULE和__KERNEL__这两个宏必须要定义,不管你是在编译时定义还是写在源文件里。我这里是两边都写了,不过加了保护,不会出现重复定义,也不会忘记定义。linux/kernel.h这个文件中,定义了KERN_ALERT这个宏,如果你直接改写成"",就可以不用包含这个头文件。而linux/module.h是模块编译必须的头文件,不可以省略。
2.6:
========================
hello.c
========================
#include
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static char *whom = "world";
module_param(whom, charp, 0);
static int howmany = 1;
module_param(howmany, int, 0);
static int hello_init(void)
{
int i;
for( i=0; i





歡迎光臨 比思論壇 (http://108.170.5.98:8080/) Powered by Discuz! X2.5