t土疙瘩 发表于 2005-1-11 16:31:16

hello_world模块加载不成功

下面是原文件和Mmakefile文件,初学者进行练习的,请大虾帮忙:
//hello2_start.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
static int init_module(void){
      printk("<1>Hello,module!\n");
      return 0;
}

//hello2_stop.c
#if defined(CONFIG_MODVERSIONS) && ! defined(MODVERSION)
        #include <linux/modversion.h>
        #define MODVERSIONS
#endif
#include <linux/kernel.h>
#include <linux/init.h>
#define __NO_VERSION__
#include <linux/module.h>
#include <linux/version.h>
static void cleanup_module(void){
      printk("<1>Goodbye, module!\n");
}
MODULE_LICENSE("GPL");

//Makefile
CC=gcc
MODCFLAGS        := -O -Wall -DMODULE -D__KERNEL__
INCLUDE := -isystem /lib/modules/`uname -r`/build/include

hello2.o:        hello2_start.o hello2_stop.o
        ld -r -o hello2.o hello2_start.o hello2_stop.o

start.o:        hello2_start.c
        ${CC} -I ${INCLUDE} $ ${MODCFLAGS} -c hello2_start.c

stop.o:                hello2_stop.c
        ${CC} -I ${INCLUDE} ${MODCFLAGS} -c hello2_start.c

编译通过,在用insmod hello2.o进行加载的时候,报错:
hello2.o: cuoldn't find the kernel version the module was compiled for

请问是为什么呢?

t土疙瘩 发表于 2005-1-11 16:42:00

又看了一下,去掉了Makefile中的-isystem,还是不行 :evil:

t土疙瘩 发表于 2005-1-11 17:09:41

我把两个文件合并成一个文件,并且去掉了
#if defined(CONFIG_MODVERSIONS) && ! defined(MODVERSION)
#include <linux/modversion.h>
#define MODVERSIONS

#define __NO_VERSION__
#include <linux/version.h>
即改成最简单的hello_module程序,
gcc -c -I ${KDIR}-o hello_world.c
编译成功,加载成功,没有警告
但是我修改上面的两个.c文件,加上了#define MODULE,
修改了Makefile,删除了-DMODULE,
编译成功,加载也成功,但是报内核的版本不匹配
但是我之前已经重新配置过内核,并且编译过了,运行的就是我编译后的内核。
我的内核版本是2.4.21的。
请问是为什么?

t土疙瘩 发表于 2005-1-11 17:51:21

FT!!
原来是拷贝的时候粗心了:start和stop用的.c文件是一样的!!!
页: [1]
查看完整版本: hello_world模块加载不成功