近日在学习< Linux 内核编程 > , 我比照书中的hellomod例子进行实验, 结果遇到了些问题, 上网搜索许久才搞定,特此记录.
工作环境
x | x |
---|---|
操作系统 | OpenSUSE 12.3 |
内核版本 | 3.7.10-1.1-desktop |
编译器 | gcc (SUSE Linux) 4.7.2 |
源文件
hellomod.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
static int __init lkp_init(void)
{
printk("<1>Hello , World : from the kernel space ... \n");
return 0;
}
static void __exit lkp_cleanup(void)
{
printk("<1>Goodbye , World : from kernel space ...\n");
return ;
}
module_init(lkp_init);
module_exit(lkp_cleanup);
书中第五行是 #MODULE_LICENSE(“GPL”); 我去掉#后才通过编译
Makefile
obj-m += hellomod.o
正确执行命令
sudo make -C /lib/modules/`uname -r`/build SUBDIRS=`pwd` modules
原书命令 :
make -C /usr/src/linux-xxx SUBDIR=$PWD modules
原书命令问题 :
- $PWD 并未被展开成当前目录
- 我的/usr/src 下有多个source目录 , 难以判断究竟使用哪个.
中途遇到的问题:
- Module.symvers missing
描述:
WARNING: Symbol version dump /usr/src/linux-xxx/Module.symvers is missing; modules will have no dependencies and modversions.
解决办法:
去对应的内核源码目录 , 执行 make modules 或者 make vmlinux
缺少文件
解决办法:去对应的内核源码目录 , 执行 make oldconfig
make prepare
make scriptsno symbol version for module_layout
这是我遇到的最痛苦的一个问题 .
解决要点:
- 确保你使用了正确的内核源文件 /lib/modules/`uname -r`/build 目录是个链接, 指向你当前运行的内核匹配的源文件目录.
- 确保你传递了正确的 SUBDIRS . 确保SUBDIRS=你的工作目录.
- 网上有一种 M=`pwd` 写法, 那个似乎是你的模块源文件处于内核目录树中的写法, 请使用 SUBDIRS.