一个关于内核模块的简单问题。我送分!!!

深水蔚蓝 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))

谁解决,送谁分!
...全文
58 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwunix 2003-03-25
  • 打赏
  • 举报
回复
其实建立一个连接就可以了,例如:
ln -s /usr/src/linux-2.4.9-9 /usr/src/linux

23,223

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧