【内核编程】【Makefile】:嵌入式驱动开发时驱动模块于内核版本不匹配的解决

 最近在进行嵌入式系统的驱动开发时,遇到内核版本与驱动模块的版本不匹配的问题。
在用insmod命令插入目标模块,会输出类似下面的错误信息


can not insmod module hello.o
for kernel is 2.4.18 rmk7
but the version of the module is 2.4.7

这些信息提示你编译的模块与目标内核的版本不一致,为了保证系统的稳定性,系统将不会允许你把这个模块插入到内核。
最容易想到的解决方法是更换内核或者编译器的版本,但这样代价将会是巨大的--这基本意味着重新构建一个系统,需要极大的工作量。
我从网络上搜索到了一些相关的信息,终于找到了一个解决这类问题的办法。


CONFIG_MODVERSIONS is a notion thought up to make people's lives
easier. In essence, what it is meant to achieve is that if you have a
module you can attempt to load that module into any kernel, safe in the
knowledge that it will fail to load if any of the kernel data
structures, types or functions that the module uses have changed.

If your kernel is not compiled with CONFIG_MODVERSIONS enabled you
will only be able to load modules that were compiled specifically for
that kernel version and that were also compiled without MODVERSIONS
enabled.

However, if your kernel is compiled with CONFIG_MODVERSIONS enabled
you will be able to load a module that was compiled for the same
kernel version with MODVERSIONS turned off. But - here's the important
part folks - you will also be able to load any modules compiled with
MDOVERSIONS turned on, as long as the kernel API that the module uses
hasn't changed.


[解释]CONFIG_MODVERSIONS是一种使人们更加方便的使用方法。它所要达到的目标是如果你要把一个模块插入到一个内核中,为了保证系统的安全,系统将会拒绝把版本不匹配的模块插入,因为有可能系统中的数据结构或者类型以及函数等发生了变化。
当你的内核在编译时没有使能(打开选项)CONFIG_MODVERSIONS,那么你将只能插入与内核匹配的模块或者不带CONFIG_MODVERSIONS选项编译出的模块。
但是当你的内核在编译时使能了CONFIG_MODVERSIONS选项,那么你插入的模块可以是一下两种情况:
1,编译时没有带CONFIG_MODVERSIONS选项,但版本必须与内核的版本一致;
2,编译时如果带有CONFIG_MODVERSIONS选项,那么模块的版本将没有限制;
而通常内核在编译是带有CONFIG_MODVERSIONS选项的,所以就出现了文章开头出现的情况:内核和模块的版本不一致。同时我们也就找到了相应的解决办法--在模块编译时选择CONFIG_MODVERSIONS选项,这样我们就可以解决版本不匹配的问题了。
我们需要这源文件(c文件)中加上下面的宏定义:


#ifdef CONFIG_MODULES
#ifdef CONFIG_MODVERSIONS
#MODFLAGS += -DMODVERSIONS -include $(HPATH)/linux/modversions.h
#endif

#endif

然后编译就可以了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值