insmod时的内核版本问题的解决方案

本文介绍了在Linux内核模块编程时遇到insmod版本不一致问题的解决方案。通过修改makefile,添加-I指定内核头文件路径及在模块中添加MODULE_LICENSE("GPL"),可以成功编译并加载模块。
insmod时的内核版本问题的解决方案
进行linux内核模块编程时,遇到了insmod时提示版本不一致的问题,经过查阅资料,发现编译时应该执行如下命令(应修改相应的linux版本号,使其与当前使用的内核源代码树的目录名一致):   

gcc -D__KERNEL__ -DLINUX -I /usr/src/linux-2.4.20-8/include -DEXPORT_SYMTAB -c testmodule.c -DMODVERSIONS -include /usr/src/linux-2.4.20-8/include/linux/modversions.h

下面是测试模块:

/*testmodule.c*/

#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>

int funModule(void )
{
 printk("<1>Message from another module!/n");
}
int *testModule;

EXPORT_SYMBOL(funModule);


int threadcode2(void *param);

int threadcode1(void *param)
{
 printk("<1>Thread 1/n");
 
 kernel_thread(threadcode2,"testparam",CLONE_FS|CLONE_FILES);
 
}
int threadcode2(void *param)
{
  char *testparam = (char *)param;
  printk("<1>Thread created by thread,recvd param: %s /n",testparam);
}
int init_module(void){
 MODULE_LICENSE("GPL");
 printk("<1>Hello,world/n");
 kernel_thread(threadcode1,NULL,CLONE_FS|CLONE_FILES);
 return 0;
}
void cleanup_module(void){
 printk("<1>Goodbye cruel world/n");
}

/*caller.c*/
#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void)
{
 MODULE_LICENSE("GPL");
 funModule();
 return 0;
}

int cleanup_module(void)
{

}

[linux,内核编程]insmod时的内核版本问题的解决方案    <转>        

本篇文章是我在Linux 操作系统上实现内核模块编译平台的笔记,现在记录下来希望对碰到和我一样问题的朋友有帮助。
首先vi hello.c
原程序来自<linux编程白皮书>:
#include <linux/kernel.h>
#include <linux/module.h>
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif

 

int init_module(void)
{
printk("Hello,World!/n");
return 0;
}

void cleanup_module(void)
{
printk("Goodbye cruel world /n");
}

原书给的makefile:

CC=gcc
MODCFLAGS:=-Wall -DMODULE-D__KERNEL__ -DLINUX
hello.o:hello.c /usr/include/linux/version.h
$(CC) $(MODCFLAGS) -c hello.c
echo insmod
echo rmmod
echo

然后输入make 命令.
然后insmod hello.o
结果出现问题:
hello.o: kernel-module version mismatch
hello.o was compiled for kernel version 2.4.20
while this kernel is version 2.4.20-8.

找了不少资料,总算解决了.
只要在makefile中gcc的参数加上-I /usr/src/linux-2.4.20-8/include (注意:linux-X.X.XX-X请根据自己的机子来配,看看/usr/src/下的linux-?就知道是什么了)

于是makefile改为:
CC=gcc
MODCFLAGS:=-Wall -DMODULE -D__KERNEL__ -DLINUX -I /usr/src/linux-2.4.20-8/include
hello.o:hello.c /usr/include/linux/version.h
$(CC) $(MODCFLAGS) -c hello.c
echo insmod
echo rmmod
echo

然后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


又是找资料,
于是知道了,在原程序中加入MODULE_LICENSE("GPL");
即:
#include <linux/kernel.h>
#include <linux/module.h>
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif


int init_module(void)
{
MODULE_LICENSE("GPL");
printk("Hello,World!/n");
return 0;
}

void cleanup_module(void)
{
printk("Goodbye cruel world /n");
}

然后make,insmod 都没问题了.

最后:
下面我们来查看这个模块所执行的显示内容,这些内容是不会在终端显示的。要执行命令:

dmesg

来查看。在最下面显示
Hello,World!
Goodbye cruel world
Hello,World!

还有一种方法可以看到这个模块是否加载成功否。

$cat /proc/modules

从中可以看到hello 这个模块,这就是我加载的。
这个文件中的信息分为四列。
第一列:模块名。第二列:模块使用内存的字节数。第三列:模块的当前使用计数。第四列:备注。
好了,我们来卸载这个模块吧。

rmmod hello

再用 dmesg 来查看卸载时发出的信息吧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值