您的位置:首页 > 其它

如何将多个源文件编译为一个.ko

2010-10-15 22:59 169 查看

如何将多个源文件编译为一个.ko

分类:
Linux driver & kernel 2010-10-15 22:59991人阅读评论(0)收藏举报

陆陆续续也写了几个Linux内核模块了,但每次都是把代码写在一个源文件中,上次尝试了写在两个.c文件中,结果没有编译通过。 无奈之下,将其中一个.c文件重命名成.h文件,再include当另一个当中。但是,在.h文件中写函数的实现总感觉怪怪的。 今天查看了以下Kbuild的文档,有如下描述

[c-sharp]
view plaincopy

If a kernel module is built from several source files, you specify  
 that you want to build a module in the same way as above.  
  
 Kbuild needs to know which the parts that you want to build your  
 module from, so you have to tell it by setting an  
 $(<module_name>-objs) variable.  
  
 Example:  
     #drivers/isdn/i4l/Makefile  
     obj-$(CONFIG_ISDN) += isdn.o  
     isdn-objs := isdn_net_lib.o isdn_v110.o isdn_common.o  
  
 In this example, the module name will be isdn.o. Kbuild will  
 compile the objects listed in $(isdn-objs) and then run  
 "$(LD) -r" on the list of these files to generate isdn.o.  

整理自己的源文件成两个.c文件(simpLB.c和sahu_lb_tools.c)、一个.h文件(sahu_lb.h)和Makefile文件。两个.c文件都包含了sahu_lb.h

按照Kbuild的文档所述,把Makefile改成如下内容:

[c-sharp]
view plaincopy

obj-m +=simpLB.o   
simpLB-objs:=sahu_lb_tools.o  
all:  
  make -C /lib/modules/`uname -r`/build M=`pwd`  
clean:  
  make -C /lib/modules/`uname -r`/build M=`pwd` clean  
install:  
  /sbin/insmod simpLB.ko  
remove:  
  /sbin/rmmod simpLB  

编译没有问题,但是安装后模块的功能没有实现,就连我在init_module()中打印的提示信息都没有。lsmod却有simpLB。

只好再上网查了查,发现如下文章:
http://www.linuxquestions.org/questions/programming-9/linking-multiple-files-kernel-module-programming-701735/
按照文章的的建议,我把Makefile修改成如下内容:

 

[c-sharp]
view plaincopy

obj-m +=sahuLB.o   
sahuLB-objs:=simpLB.o sahu_lb_tools.o  
all:  
  make -C /lib/modules/`uname -r`/build M=`pwd`  
clean:  
  make -C /lib/modules/`uname -r`/build M=`pwd` clean  
install:  
  /sbin/insmod sahuLB.ko  
remove:  
  /sbin/rmmod sahuLB 

----------------------------------

hell.ko的编译过程

1 源代码书写要保证正确,MODULE_LICENSE();是一条语句,后面要有分号;

2 static void hello_exit()的返回类型为void;

3 make M=`pwd` M参数后面不是单引号,而是斜点``;

4 -c 指定包含kernel顶级Makefile的源码目录;

5 源代码要执行make menuconfig生成默认配置,无须修改任何内容;

6 make menuconfig 的运行可能需要安装necurses-dev;

7 生成ko模块文件所使用的源码版本要和insmod的目标内核版本一致;

8 2.6个版本内核下载 http://www.kernel.org/pub/linux/kernel/v2.6/;
9 make编译可能提示没有modpost等脚本文件,可以从其它源码目录拷到当前使用源码目录下,或者利用当前源码目录下的c文件直接编译生成;

10 如果insmod过程中出错,可以使用dmesg | tail 命令查看系统日志

11 模块的编译和插入,对版本一致性要求很严格,如果编译依赖的内核源码版本和目标平台正在运行的内核版本不一致的话,很可能模块无法正常insert,出现诸如“-1 Invalid module format”或“no symbol version for module layout”的错误

所以编译过程中:
不是为正在运行的内核编译模块:

make -C <path-to-kernel> M='pwd'

为正在运行的内核编译模块:

make -C /lib/modules/'uname -r'/build M='pwd'

即可以保证insmod无误;

分享到:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: