最简单的模块编程编译出错了?!新手请教…《Linux设备驱开发详解》(宋宝华编著) 上的一个简单例子…
第一次接触driver,出错了不知如何下手……
-----------------------------------------------------------------
bash-3.1# pwd
/usr/local-data/code/drivers/hello_module
bash-3.1# ls
hello.cMakefile
bash-3.1# cat hello.c
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int__init hello_enter(void)
{
printk("Hello World -- enter\n");
return 0;
}
static void __exit hello_exit(void)
{
printk("Hello World -- exit\n");
}
module_init(hello_enter);
module_exit(hello_exit);
MODULE_AUTHOR("Baohua Song");
MODULE_DESCRIPTION("A simplest module!");
MODULE_ALIAS("a simplest module");
bash-3.1# cat Makefile
obj-m := hello.o
bash-3.1# make -C /usr/src/linux-2.6.24M=/usr/local-data/code/drivers/hello_module/ modules
make: Entering directory `/usr/src/linux-2.6.24'
WARNING: Symbol version dump/usr/src/linux-2.6.24/Module.symvers
is missing; modules will have no dependencies andmodversions.
CC /usr/local-data/code/drivers/hello_module/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /usr/local-data/code/drivers/hello_module/hello.mod.o
/usr/local-data/code/drivers/hello_module/hello.mod.c:8: 错误:变量‘__this_module’ 有初始值设定但类型不完全
/usr/local-data/code/drivers/hello_module/hello.mod.c:9:错误:初始值设定项里有未知的字段 ‘name’
/usr/local-data/code/drivers/hello_module/hello.mod.c:9:警告:结构初始值设定项中有多余元素
/usr/local-data/code/drivers/hello_module/hello.mod.c:9: 警告:(在‘__this_module’ 的初始化附近)
/usr/local-data/code/drivers/hello_module/hello.mod.c:10:错误:初始值设定项里有未知的字段 ‘init’
/usr/local-data/code/drivers/hello_module/hello.mod.c:10:警告:结构初始值设定项中有多余元素
/usr/local-data/code/drivers/hello_module/hello.mod.c:10: 警告:(在‘__this_module’ 的初始化附近)
/usr/local-data/code/drivers/hello_module/hello.mod.c:14:错误:初始值设定项里有未知的字段 ‘arch’
/usr/local-data/code/drivers/hello_module/hello.mod.c:14:错误:‘MODULE_ARCH_INIT’ 未声明 (不在函数内)
/usr/local-data/code/drivers/hello_module/hello.mod.c:14:警告:结构初始值设定项中有多余元素
/usr/local-data/code/drivers/hello_module/hello.mod.c:14: 警告:(在‘__this_module’ 的初始化附近)
make: *** 错误 1
make: *** 错误 2
make: Leaving directory `/usr/src/linux-2.6.24'
bash-3.1# ls
hello.chello.mod.chello.oMakefileModule.symvers
bash-3.1# cat hello.mod.c
#include
#include
#include
MODULE_INFO(vermagic, VERMAGIC_STRING);
struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
static const char __module_depends[]
__attribute_used__
__attribute__((section(".modinfo"))) =
"depends=";
[ 本帖最后由 Kallawa 于 2008-8-25 18:42 编辑]myblog:http://blog.chinaunix.net/u1/52350/showart_441201.html看了你的文章,“要求:已经安装开发工具,在usr/src/下有内核源代码,我这里是安装开发工具时自带的”,请问,“开发根据”具体指的是哪些?
认真比较了源代码和Makefile,没有多大差异的……
/usr/src/linux-2.6.24是linux-2.6.24.tar.bz2直接解压的源码,不知这是否有影响?“开发工具”---回复#3 Kallawa 的帖子需要先编译内核. 具体可以到内核版去看是怎样编译内核的.感觉是编译方法有问题。
试试这个 Makefile:
# Makefile
TARGET=hello
obj-m:=${TARGET}.o
KDIR:=/usr/src/linux-2.6.24
PWD:=$(shell pwd)
default %:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
存为 Makefile,然后在命令行输入:
make
[ 本帖最后由 flw 于 2008-4-22 12:15 编辑]http://bbs.chinaunix.net/viewthread.php?tid=1064471是不是
make -C /usr/src/linux-2.6.24M=/usr/local-data/code/drivers/hello_module/ modules
这句出问了?
要是使用系统内核呢?
/lib/modules/2.6.21.5-smp/build链接到www.dgdaming.com:
build -> /usr/src/linux-2.6.21.5
可并没有/usr/src/linux-2.6.21.5这个文件夹(安装系统的时候要么不带源码,要么删掉了)谢谢xi2008wang,scutan,flw,兔子的回帖……
看了几位的Makefile,个人觉得在make -C /usr/src/linux-2.6.24M=/usr/local-data/code/drivers/hello_module/ modules这句里出问题了!
(KERNEL_DIR:=/lib/modules/$(KERNEL_VERSION)/build链接到/usr/src/linux-$(KERNEL_VERSION))
注意www.hc3600.com,我的机子上的/usr/src/linux-2.6.24是直接解压出来的!
很可能象scutan所说的要编译内核……不知道有没有不编译内核的方法??
附运行flw的Makefile后显示error:
bash-3.1# make
make -C /usr/src/linux-2.6.24SUBDIRS=/usr/local-data/code/drivers/hello_module modules
make: Entering directory `/usr/src/linux-2.6.24'
WARNING: Symbol version dump/usr/src/linux-2.6.24/Module.symvers
is missing; modules will have no dependencies andmodversions.
scripts/Makefile.build:212:目标“/usr/local-data/code/drivers/hello_module/.O”不匹配目标模式
CC /usr/local-data/code/drivers/hello_module/.O
gcc: 没有输入文件
make: *** 错误 1
make: *** 错误 2
make: Leaving directory `/usr/src/linux-2.6.24'
make: *** 错误 2你把 makefile 没搞对吧。
估计是漏了 TARGET 那一行,再要不就是拼错了。
第一次接触driver,出错了不知如何下手……
-----------------------------------------------------------------
bash-3.1# pwd
/usr/local-data/code/drivers/hello_module
bash-3.1# ls
hello.cMakefile
bash-3.1# cat hello.c
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int__init hello_enter(void)
{
printk("Hello World -- enter\n");
return 0;
}
static void __exit hello_exit(void)
{
printk("Hello World -- exit\n");
}
module_init(hello_enter);
module_exit(hello_exit);
MODULE_AUTHOR("Baohua Song");
MODULE_DESCRIPTION("A simplest module!");
MODULE_ALIAS("a simplest module");
bash-3.1# cat Makefile
obj-m := hello.o
bash-3.1# make -C /usr/src/linux-2.6.24M=/usr/local-data/code/drivers/hello_module/ modules
make: Entering directory `/usr/src/linux-2.6.24'
WARNING: Symbol version dump/usr/src/linux-2.6.24/Module.symvers
is missing; modules will have no dependencies andmodversions.
CC /usr/local-data/code/drivers/hello_module/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /usr/local-data/code/drivers/hello_module/hello.mod.o
/usr/local-data/code/drivers/hello_module/hello.mod.c:8: 错误:变量‘__this_module’ 有初始值设定但类型不完全
/usr/local-data/code/drivers/hello_module/hello.mod.c:9:错误:初始值设定项里有未知的字段 ‘name’
/usr/local-data/code/drivers/hello_module/hello.mod.c:9:警告:结构初始值设定项中有多余元素
/usr/local-data/code/drivers/hello_module/hello.mod.c:9: 警告:(在‘__this_module’ 的初始化附近)
/usr/local-data/code/drivers/hello_module/hello.mod.c:10:错误:初始值设定项里有未知的字段 ‘init’
/usr/local-data/code/drivers/hello_module/hello.mod.c:10:警告:结构初始值设定项中有多余元素
/usr/local-data/code/drivers/hello_module/hello.mod.c:10: 警告:(在‘__this_module’ 的初始化附近)
/usr/local-data/code/drivers/hello_module/hello.mod.c:14:错误:初始值设定项里有未知的字段 ‘arch’
/usr/local-data/code/drivers/hello_module/hello.mod.c:14:错误:‘MODULE_ARCH_INIT’ 未声明 (不在函数内)
/usr/local-data/code/drivers/hello_module/hello.mod.c:14:警告:结构初始值设定项中有多余元素
/usr/local-data/code/drivers/hello_module/hello.mod.c:14: 警告:(在‘__this_module’ 的初始化附近)
make: *** 错误 1
make: *** 错误 2
make: Leaving directory `/usr/src/linux-2.6.24'
bash-3.1# ls
hello.chello.mod.chello.oMakefileModule.symvers
bash-3.1# cat hello.mod.c
#include
#include
#include
MODULE_INFO(vermagic, VERMAGIC_STRING);
struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
static const char __module_depends[]
__attribute_used__
__attribute__((section(".modinfo"))) =
"depends=";
[ 本帖最后由 Kallawa 于 2008-8-25 18:42 编辑]myblog:http://blog.chinaunix.net/u1/52350/showart_441201.html看了你的文章,“要求:已经安装开发工具,在usr/src/下有内核源代码,我这里是安装开发工具时自带的”,请问,“开发根据”具体指的是哪些?
认真比较了源代码和Makefile,没有多大差异的……
/usr/src/linux-2.6.24是linux-2.6.24.tar.bz2直接解压的源码,不知这是否有影响?“开发工具”---回复#3 Kallawa 的帖子需要先编译内核. 具体可以到内核版去看是怎样编译内核的.感觉是编译方法有问题。
试试这个 Makefile:
# Makefile
TARGET=hello
obj-m:=${TARGET}.o
KDIR:=/usr/src/linux-2.6.24
PWD:=$(shell pwd)
default %:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
存为 Makefile,然后在命令行输入:
make
[ 本帖最后由 flw 于 2008-4-22 12:15 编辑]http://bbs.chinaunix.net/viewthread.php?tid=1064471是不是
make -C /usr/src/linux-2.6.24M=/usr/local-data/code/drivers/hello_module/ modules
这句出问了?
要是使用系统内核呢?
/lib/modules/2.6.21.5-smp/build链接到www.dgdaming.com:
build -> /usr/src/linux-2.6.21.5
可并没有/usr/src/linux-2.6.21.5这个文件夹(安装系统的时候要么不带源码,要么删掉了)谢谢xi2008wang,scutan,flw,兔子的回帖……
看了几位的Makefile,个人觉得在make -C /usr/src/linux-2.6.24M=/usr/local-data/code/drivers/hello_module/ modules这句里出问题了!
(KERNEL_DIR:=/lib/modules/$(KERNEL_VERSION)/build链接到/usr/src/linux-$(KERNEL_VERSION))
注意www.hc3600.com,我的机子上的/usr/src/linux-2.6.24是直接解压出来的!
很可能象scutan所说的要编译内核……不知道有没有不编译内核的方法??
附运行flw的Makefile后显示error:
bash-3.1# make
make -C /usr/src/linux-2.6.24SUBDIRS=/usr/local-data/code/drivers/hello_module modules
make: Entering directory `/usr/src/linux-2.6.24'
WARNING: Symbol version dump/usr/src/linux-2.6.24/Module.symvers
is missing; modules will have no dependencies andmodversions.
scripts/Makefile.build:212:目标“/usr/local-data/code/drivers/hello_module/.O”不匹配目标模式
CC /usr/local-data/code/drivers/hello_module/.O
gcc: 没有输入文件
make: *** 错误 1
make: *** 错误 2
make: Leaving directory `/usr/src/linux-2.6.24'
make: *** 错误 2你把 makefile 没搞对吧。
估计是漏了 TARGET 那一行,再要不就是拼错了。