一个关于内核模块的简单问题。我送分!!!
深水蔚蓝 2003-03-25 09:06:31 我要写一个hello,world的内核模块程序,如下:
/* hello.c */
#include <linux/kernel.h>
#include <linux/module.h>
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
int init_module()
{
printk("Hello, World - this is the kernel speaking\n");
return 0;
}
void cleanup_module()
{
printk("Short is the life of a kernel module\n");
}
然后用一个make编译:
# Makefile for a basic kernel module
CC=gcc
MODCFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX
hello.o: hello.c /usr/include/linux/version.h
$(CC) $(MODCFLAGS) -c hello.c
make后生成hello.o。
$>insmod hello.o
hello.o: kernel-module version mismatch
hello.o was compiled for kernel version 2.4.9-9
while this kernel is version 2.4.18-14.
不能运行,要怎么解决版本匹配的问题?
然后我把程序的前面改为:
/* hello.c */
#include <linux/kernel.h>
#define __NO_VERSION__i /* 增加的 */
#include <linux/module.h>
#include <linux/version.h> /* 增加的 */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
...
make后,
$>insmod hello.o
Warning: loading hello.o will taint the kernel: no license
See http://www.tux.org/lkml/#export-tainted for information about tainted modules
Module hello loaded, with warnings
并没有所要的打印(这个Warning我很迷惑)
Hello, World - this is the kernel speaking
两种情况卸载module
$>rmmod hello
都没有打印
Short is the life of a kernel module
我的linux为redhat 8.0 ,另外version.h为
/* /usr/include/linux/version.h */
#define UTS_RELEASE "2.4.9-9"
#define LINUX_VERSION_CODE 132105
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
谁解决,送谁分!