您的位置:首页 > 其它

hello world 模块编译

2015-08-04 13:39 429 查看

http://blog.csdn.net/wangkaiblog/article/details/7435423

2012-04-07 16:57 772人阅读
评论(0)
收藏
举报

makefilemoduleubuntushelllayoutc

[cpp]
view plaincopy

   

[cpp]
view plaincopy

1、test.c  

[cpp]
view plaincopy

#include <linux/init.h>  
#include <linux/module.h>  
  
static int hello_init(void)  
{  
    printk(KERN_INFO "Hello World enter \n");  
}  
  
static int hello_exit(void)  
{  
    printk(KERN_INFO "Hello World exit\n");  
}  
  
module_init(hello_init);  
module_exit(hello_exit);  

2、Makefile

[html]
view plaincopy

obj-m :=test.o  

3、在shell 中键入

 

[cpp]
view plaincopy

make -C /usr/src/linux-3.3.1/linux-3.3.1/ M=~/Documents/test/ <span style="color:#ff0000;">modules</span>  

 

 

4、在执行时,出现下面错误:

insmod: error inserting './test.ko': -1 Invalid module format

 

出错信息出现在 /var/log/message 中。

Apr  7 16:27:33 ubuntu kernel: [ 7026.609031] test: disagrees about version of symbol module_layout

 

而模块的相关信息可以通过 modinfo 来看。

 

[cpp]
view plaincopy

wangkai@ubuntu:~/Documents/test$ sudo modinfo test.ko  
filename:       test.ko  
license:        Dual BSD/GPL  
srcversion:     A683B0513DFC18A40C79C30  
depends:          
vermagic:       3.3.1-20120406 SMP mod_unload modversions 68  

 

内核无法加载模块的原因是因为记载版本号的字符串和当前正在运行的内核模块的不一样,这个版本印戳作为一个静态的字符串存在于内核模块中,叫vermagic
真正的原因是由于当前的内核源码和系统版本不相匹配。 当前正在运行的系统的 为ubuntu 10.10的内核。而源码则为 3.3.1的源码。故出错。

 

5、解决问题的方法 是

[cpp]
view plaincopy

$ make -C /usr/src/linux-headers-2.6.35-22-generic/  M=~/Documents/test/ modules  

二、通过写Makefile的方法,使得模块的勾走更加简单。

1、最简单的方法是,把上面的命令写进Makefile中即可。

[cpp]
view plaincopy

obj-m :=test.o  
  
kernel_modules:  
     make -C /usr/src/linux-headers-2.6.35-22-generic/  M=/home/wangkai/Documents/test/ modules  
  
clean:  
     make -C /usr/src/linux-headers-2.6.35-22-generic/  M=/home/wangkai/Documents/test/ clean  

 

2、但是这种方式,不够通用,可以写得更加通用些。

[cpp]
view plaincopy

KVER = $(shell uname -r)  
  
obj-m :=test.o  
  
kernel_modules:  
     make -C /lib/modules/$(KVER)/build  M=$(PWD) modules  
  
clean:  
     make -C /lib/modules/$(KVER)/build  M=$(PWD) clean  

 

编译时,只需

[csharp]
view plaincopy

make  

重新编译时,只需

[cpp]
view plaincopy

make clean  

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