今天编译 在SUSE 10.3 (kernel 2.6) 上的 模块module ,多次未果.几经修改 Makefile 如下 (Makefile 首字母大写)
ifneq ($(KERNELRELEASE),)
obj -m := hello.o
else
obj -m := hello.o
KDIR := /lib/modules/$(shell uname -r)/build
#这里的 核心目录 原来是 /usr/src/linux-2.6.22.5-31
#今天试了试 用 /usr/src/linux-2.6.22.5-31 编译也可以,只是会提示警告没有符号版本信息
all:
$(MAKE) -C $(KDIR) M=$(PWD)
clean:
rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions
endif
编译还是不能生成 .ko .o 文件
只是输出
……
Building modules,stage 2.
MODPSOT 0 modules
……
抱着最后希望 将obj -m 合并为obj-m 结果通过,呵呵,真是弄人,剩下些编译问题,调
通过insmod /rmmod 后 dmesg | tail
总结出几点注意 (1) obj -m 要合起来写
(2) 核心目录是 /lib/modules/$(shell uname -r)/build
或 /usr/src/linux-2.6.22.5-31
(3) exit 函数是 void 类型的
(4) 要建立一个目录把.c 和Makefile 放进去 名字怎么取就要再实践了:)
此外开始的内核代码需要配置一下,
看了书后,照着书上的方法一步一步去做,却失败了,555555555555555
真是的,写书的人啊,却不考虑一下细节问题
新建一个目录
[liu@liu-desktop hellomod]$mddir hellomod
[liu@liu-desktop hellomod]$cd hellomod
[liu@liu-desktop hellomod]$vi hellomod.c
/****************hellomod.c*******************************/
#include //所有模块都需要的头文件
#include // init&exit相关宏
MODULE_LICENSE("GPL");
static int __init hello_init (void)
{
printk("Hello china init\n");
return 0;
}
static void __exit hello_exit (void)
{
printk("Hello china exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
/****************hellomod.c*******************************/
1、在下载了linux 2.6的内核,解压到/usr/src/linux26目录下
[root@liu-desktop linux26]# ls
arch CREDITS drivers init kernel Makefile README security
block crypto fs ipc lib mm REPORTING-BUGS sound
COPYING Documentation include Kbuild MAINTAINERS net scripts usr
[liu@liu-desktop hellomod]$ make -C /usr/src/linux26/ SUBDIRS=$PWD modules
make: Entering directory `/usr/src/linux26'
ERROR: Kernel configuration is invalid.
include/linux/autoconf.h or include/config/auto.conf are missing.
Run 'make oldconfig && make prepare' on kernel src to fix it.
WARNING: Symbol version dump /usr/src/linux26/Module.symvers
is missing; modules will have no dependencies and modversions.
CC [M] /home/liu/test/hellomod/hellomod.o
cc1: 错误: include/linux/autoconf.h:No such file or directory
在包含自 include/linux/posix_types.h:47 的文件中,
从 include/linux/types.h:14,
从 include/linux/prefetch.h:13,
从 include/linux/list.h:8,
从 include/linux/module.h:9,
从 /home/liu/test/hellomod/hellomod.c:1:
/usr/lib/gcc/i486-linux-gnu/4.1.3/include/asm/posix_types.h:13:22: 错误: features.h:No such file or directory
/usr/lib/gcc/i486-linux-gnu/4.1.3/include/asm/posix_types.h:14:35: 错误: 没有包含路径可供搜索 asm/posix_types.h
...............................
解决方法:
[liu@liu-desktop hellomod]#make oldconfig
[liu@liu-desktop hellomod]#make prepare
好了,在试试:
[liu@liu-desktop hellomod]$ make -C /usr/src/linux26/ SUBDIRS=$PWD modules
还是有错:
make: Entering directory `/usr/src/linux26'
WARNING: Symbol version dump /usr/src/linux26/Module.symvers
is missing; modules will have no dependencies and modversions.
CC [M] /home/liu/test/hellomod/hellomod.o
Building modules, stage 2.
MODPOST 1 modules
/bin/sh: scripts/mod/modpost: not found
make[1]: *** [__modpost] 错误 127
make: *** [modules] 错误 2
make: Leaving directory `/usr/src/linux26'
看到了吗,提示说没有scripts/mod/modpost,那我们就编译它吧
[root@liu-desktop linux26]# make scripts
HOSTCC scripts/genksyms/genksyms.o
SHIPPED scripts/genksyms/lex.c
SHIPPED scripts/genksyms/parse.h
SHIPPED scripts/genksyms/keywords.c
HOSTCC scripts/genksyms/lex.o
SHIPPED scripts/genksyms/parse.c
HOSTCC scripts/genksyms/parse.o
HOSTLD scripts/genksyms/genksyms
CC scripts/mod/empty.o
HOSTCC scripts/mod/mk_elfconfig
MKELF scripts/mod/elfconfig.h
HOSTCC scripts/mod/file2alias.o
HOSTCC scripts/mod/modpost.o
HOSTCC scripts/mod/sumversion.o
HOSTLD scripts/mod/modpost
HOSTCC scripts/kallsyms
HOSTCC scripts/conmakehash
OK,好了
[liu@liu-desktop hellomod]$ make -C /usr/src/linux26/ SUBDIRS=$PWD modules
make: Entering directory `/usr/src/linux26'
WARNING: Symbol version dump /usr/src/linux26/Module.symvers
is missing; modules will have no dependencies and modversions.
Building modules, stage 2.
MODPOST 1 modules
CC /home/liu/test/hellomod/hellomod.mod.o
LD [M] /home/liu/test/hellomod/hellomod.ko
make: Leaving directory `/usr/src/linux26'
[liu@liu-desktop hellomod]$ ls
hellomod.c hellomod.ko hellomod.mod.c hellomod.mod.o hellomod.o Makefile Module.symvers
[root@liu-desktop linux26]#insmod hellomod.ko
[root@liu-desktop linux26]#lsmod |grep hellomod
lsmod |grep hellomod
[root@liu-desktop linux26]#rmmod hellomod
阅读(5359) | 评论(1) | 转发(0) |