hit007 发表于 2003-4-5 22:16:50

内核模块编译,加载时说版本不对的问题

我编了一个简单的内核模块
//helloworld.c

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/version.h>

int init_module(void)
{
    printk("<1>Hello World!\n");
    return 0;
}


void cleanup_module(void)
{
    printk("<1>Goodbye");
}


我用gcc编译如下:
gcc -c -DMODULE -D__KERNEL__-DLINUX helloworld.c
结果如下:
helloworld.c:18:1: warning: no newline at end of file

然后插入模块:
insmod helloworld.o

结果如下:
helloworld.o: kernel-module version mismatch
      helloworld.o was compiled for kernel version 2.4.9-9
      while this kernel is version 2.4.18-14.

如果我用参数 -f强行加载
insmod -f helloworld.0


结果如下:

Warning: kernel-module version mismatch
      helloworld.o was compiled for kernel version 2.4.9-9
      while this kernel is version 2.4.18-14
Warning: loading helloworld.o will taint the kernel: no license
See http://www.tux.org/lkml/#export-tainted for information about tainted modules
Warning: loading helloworld.o will taint the kernel: forced load
Module helloworld loaded, with warnings


之所以没有出现<1>Hello World!是因为没有在真正的终端运行.


我的问题是如果不强行使用-f ,应该怎么办?



我的系统:
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)

hit007 发表于 2003-4-5 22:41:05

怎么没有人回复?

Dragonfly 发表于 2003-4-6 03:53:51

this is because the kernel code u compile with your helliworld is the different with u running code.
1) from "uname -a" u can know the version of u running kernel, here we know that u are using the 2.4.18-4, a redhat hacked one.
2) you use gcc only, let's assume u code use /usr/src/linux. since /usr/src/linux is usually a symbol link to a real kernel source code, for example /usr/src/linux-2.4.x-x, pls check what actual linux kernel code u are using. i guess u are using 2.4.9-9.
3) we seldom use gcc directly, most of the time we will use a Makefile. I will post a sample makefile later.
4) i recommend u run your system with a standard(vanilla) kernel download from kernel.org directly. since mant people dislike the hacked one from redhat. you need download, recompile, and install the kernel source and use the new compiled kernel as u running kernel.
5) u can add a newline at the end of u .c file to remove that newline warning.
6) u can add "MODULE_LICENSE("GPL");' at the end of u .c file to remove that license warning.

Dragonfly 发表于 2003-4-6 03:54:49

a sample Makefile

#INCLUDEDIR = ../linux/include
INCLUDEDIR = /usr/src/linux/include

DEBFLAGS = -O2

CFLAGS = -D__KERNEL__ -DMODULE -Wall $(DEBFLAGS)
CFLAGS += -I$(INCLUDEDIR)

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

all: $(TARGET).o

clean:
        rm -f *.o *~ core

hit007 发表于 2003-4-6 13:01:05

有个问题:
1>你说我用的内核源码是从usr/src下的,我的这个目录下有两个目录:
    linux-2.4   
    linux-2.4.18-14
    redhat
    其中linux-2.4指向linux-2.4.18-14
    那么我用的source code version应该也是2.4.18-14的
2>我运行的内核
    用uname -a,结果如下:
   Linux localhost.localdomain 2.4.18-14 #1 Wed Sep 4 13:35:50      
   EDT 2002 i686 i686 i386 GNU/Linux
   说明我的source code 和running code 版本一致
3> 我买的是盒装的redhat 8.0,好像应该是正版的吧,不会是hacked one.
4>那我的问题究竟怎么解决?
5>如何add a newline to remove warning :no newline at end of file,不是回车吗?我加了回车,没用.

llc 发表于 2003-4-6 19:46:06

这个问题最近见过几次,不知道究竟怎么回事会变成2.4.9的内核编译,实在不解

Dragonfly 发表于 2003-4-6 23:30:59

yes, so u kernel code under /usr/src/ is 2.4.18-4.
1) can u check u /usr/src/linux-2.4/Makefile to see the first 4 lines. It should be
VERSION = 2
PATCHLEVEL = 4
SUBLEVEL = 18
EXTRAVERSION = -14.
2) can u check u /usr/src/linux-2.4/include/linux/version.h, to see if it also define 2.4.18-4.
3) have you run the make config before. for kernel module developing, you need run the make config and make menuconfig to generate correct version info.
4) i suggest you use a standard(vanilla) kernel.
5) that warning doesnot have any effect on u module running, so ignore it if u can not remove it.

hit007 发表于 2003-4-7 09:09:35

我的各个文件配置如你所述,没有问题.
但是问题还是没有解决

Dragonfly 发表于 2003-4-7 10:44:07

have you run the make config before?

can u use that makefile and try again? that can make sure u use the include file under /usr/src/linux.

can u use a vanilla kernel and try again?

llc 发表于 2003-4-7 11:55:40

#include "/linux/modversions.h"
试试

Dragonfly 发表于 2003-4-7 22:00:19

that modversion.h should be included when u include module.h

hit007 发表于 2003-4-10 13:20:00

i haven't run make config once
should i run it ?
how?
then the problem can be resolved ?

hit007 发表于 2003-4-11 17:38:55

who can help me
now i have install rh9.0,but the problem is the same

Dragonfly 发表于 2003-4-11 21:46:11

i haven't run make config once
should i run it ?
how?
then the problem can be resolved ?

ic. u at least should run the "make dep" to generate correct header info.

Dragonfly 发表于 2003-4-11 21:48:14

u had better learn how to compile the kernel before learn the kernel module programming.
页: [1] 2 3
查看完整版本: 内核模块编译,加载时说版本不对的问题