QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2164|回复: 19

内核模块版本?

[复制链接]
发表于 2003-5-17 14:20:14 | 显示全部楼层 |阅读模式
Hi,guys
第一次接触linux的内核模块编程,典型的“hello,world”。:p
第一次编译的时候没有加入/usr/include/linux/version.h,不行。
第二次加了,发现版本不对,不能insmod.
第三次修改version.h, change "2.4.9-9" to "2.4.20", 这样才能insmod
是不是linux对于版本号有必须要求?
比如都是2.4.x的内核,不同x之间编译的内核不能在2.4.x中二进制复用?
有没有办法,让系统略过version的检查?

谢谢!
发表于 2003-5-17 15:07:17 | 显示全部楼层
楼上装的是什么版本?
我装的是redhat8。0,内核是2.4.18-14
修改了version.h, change "2.4.9-9" to "2.4.18-14", 仍然不能insmod
无比茫然阿 :neutral:
回复

使用道具 举报

 楼主| 发表于 2003-5-17 15:25:45 | 显示全部楼层
RH8,我update kernel到2.4.20了。
我用RH8的缺省内核试了一下,把其改为2.4.18-14是可以的。我想你的Makefile写的有问题。我的贴出来,你参考:
CC=gcc
MODCFLAG := -O6 -Wall -DCONFIG_KERNELD -DMODULE -D_KERNEL__ -DLinux

helloworld.o:   helloworld.c /usr/include/linux/version.h
                $(CC) $(MODCFLAG) -c helloworld.c
回复

使用道具 举报

发表于 2003-5-17 15:33:44 | 显示全部楼层
见精华区的帖子,有这个例子的说明
回复

使用道具 举报

 楼主| 发表于 2003-5-17 17:04:42 | 显示全部楼层
我看了,dragonfly说的很清楚,我又改了我的Makefile
INCLUDEDIR = /usr/src/linux/include

CC = gcc
DEBFLAG = -O2

CFLAG = -Wall -DCONFIG_KERNELD -DMODULE -D_KERNEL__ -DLinux $(DEBFLAG)
CFLAG += -I$(INCLUDEDIR)

TARGET = helloworld
OBJS = $(TARGET).o
SRC = $(TARGET).c

all:
        $(CC) $(CFLAG) -c $(SRC) -o $(TARGET).o
clean:
        rm -f *.o *.core

我不知道为什么引用dragonfly的Makefile不行。 :-(
但是不同内核版本的模块必须加f才能加载,比如:2.4.20编译的,在2.4.18下,必须加-f才能加载。
回复

使用道具 举报

发表于 2003-5-17 17:07:05 | 显示全部楼层
我不会用makefile的
以前gcc -I usr/src/linux-2.4.18-14/include -c hello.c时出现"kernel-module version mismatch"的问题.

看了精华贴,用这个命令
gcc -c -DMODULE -D__KERNEL__ -DLINUX -I/usr/src/linux-2.4.18-14/include hello.c
结果:hello.c:1:1: warning: "MODULE" redefined
hello.c:1:1: warning: this is the location of the previous definition
再输入命令insmod ./hello.o
结果:
insmod: a module named hello already exists
这是什么意思?
回复

使用道具 举报

 楼主| 发表于 2003-5-17 17:13:43 | 显示全部楼层
1. 我觉得你的源码可能有问题
你对照这个源码检查一下:
#include <linux/kernel.h>
#include <linux/module.h>

int init_module(void)
{
        printk("<1>hello, world\n");
        return 0;
}

void cleanup_module(void)
{
        printk("<1>bye, bye\n");
}

MODULE_LICENSE("GPL");

2. gcc命令:
gcc -Wall -DCONFIG_KERNELD -DMODULE -D_KERNEL__ -DLinux -O2 \
-I/usr/src/linux/include -c helloworld.c -o helloworld.o

3. 你的insmod出错,可能是由于你已经加载了一个叫hello的内核模块,出现重名了。

4. 建议你学着写Makefile,这是基本技能,慢慢积累,不用想一开始就写出很pp很大的Makefile。
回复

使用道具 举报

发表于 2003-5-17 20:21:36 | 显示全部楼层
多谢学长,我的程序是从LDD这本书上抄的。
对照你给出的,我发现少了最开始句#include <linux/kernel.h>和最末尾句MODULE_LICENSE("GPL");
我再试试吧
Makefile我也想学的。
回复

使用道具 举报

发表于 2003-5-18 10:29:01 | 显示全部楼层
this is the minimal one can work
[code:1]
#include <linux/module.h>
#include <linux/slab.h>

int hello_init(void)
{
        printk("module init\n");
        return 0;
}

void hello_clean(void)
{
        printk("module cleanup\n");
}

module_init(hello_init);
module_exit(hello_clean);

MODULE_LICENSE("GPL");
[/code:1]
version.h will be included automatically.
need check the /usr/src/linux/include. whether the /usr/src/linux is u linux source code dir. must match. the default rh kernel source code will add 2.xx.xcustom. need remove that custom.
since there are many difference between each kernel version. kernel have to use the kernel version code to make sure u will call correct functions.
write makefile is a good habit. i bet my makefile can work.  
for the "already exist " problem, u already insert the module to kernel. u can use lsmod to check it. and u can use rmmod to remove it.

btw, i go to cinema to see the MATRIX II yesterday night. so do not reply the posts in time. sorry.  
回复

使用道具 举报

发表于 2003-5-18 10:31:18 | 显示全部楼层
jjww, i wonder if u code can compile ok without including the slab.h.
回复

使用道具 举报

 楼主| 发表于 2003-5-18 10:49:10 | 显示全部楼层
yes,我没有用slab.h,在2.4.20下编译的,没有问题呀。
回复

使用道具 举报

发表于 2003-5-18 10:58:45 | 显示全部楼层
strange, if i do not include it, i compile it with warning, although get .o as well. anyway  u need that for kmalloc. i can not image a serious module do not have kmalloc.
回复

使用道具 举报

发表于 2003-5-18 13:14:58 | 显示全部楼层
OK了,多谢两位
Dragonfly,你好爽啊,已经看了MATRIX II

:-)
回复

使用道具 举报

 楼主| 发表于 2003-5-18 13:17:59 | 显示全部楼层
是吗?在FreeBSD里不需要的,除非我们自己定义的数据结构需要动态分配内存,需要通过MALLOC_DECLARE宏声明,象printk这种内核公用函数,我觉得linux应该在启动的时候就应该定义好slab.
回复

使用道具 举报

发表于 2003-5-18 13:22:23 | 显示全部楼层
我的源程序是这样的,是不是有问题呀?
#define MODULE
#include <linux/module.h>
int init_module(void) {printk("<1>hello world\n");return 0;}
void cleanup_module(void){printk("<1>goodbye\n");}

用这个命令:gcc -I usr/src/linux-2.4.18-14/include -c hello.c
没问题,再用命令:insmod ./hello.o
返回:hello.o is compile for kernel version 2.4.9-9
       while this kernel version is 2.4.18-14
  
我想问的是是不是程序本身的写法只能用于2.4.9-9
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-5-22 23:04 , Processed in 0.172843 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表