免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2991 | 回复: 4
打印 上一主题 下一主题

设备驱动程序开发的编译问题 ~先谢谢大家帮忙啊 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-10-12 20:27 |只看该作者 |倒序浏览
先谢谢大家的帮忙了啊

/*
*        Hello world module.
*/
//#include <linux/init.h>

#include <linux/module.h>

#if defined(CONFIG_SMP)
#define __SMP__
#endif

#if defined(CONFIG_MODVERSIONS)
#define MODVERSIONS
#include <linux/modversions.h>
#endif

#include <linux/kernel.h>

static __init int init_module(void)
{
        printk(KERN_DEBUG "Hello, kernel!\n");
        return 0;
}

static __exit void cleanup_module(void)
{
        printk(KERN_DEBUG "Good-bye, kernel!\n");
}

这个是Linux程序设计上设备驱动程序开发的代码
我是在虚拟机上安装的CentOS-4.6

我按照书上的编译命令
gcc -D__KERNEL__-DMODULE -DMODVERSIONS -I /user/src/kernels/2.6.9-67.EL-smp-i686/include -Wall -O2  -o hello -c hello.c
但是出现了 <command line>:1:11: warning: ISO C requires whitespace after the macro name
In file included from /usr/include/linux/module.h:10,
                 from hello.c:6:
/usr/include/linux/config.h:5:2: #error Incorrectly using glibc headers for a kernel module
hello.c:19: error: syntax error before "int"
hello.c: In function `init_module':
hello.c:21: warning: implicit declaration of function `printk'
hello.c:21: error: `KERN_DEBUG' undeclared (first use in this function)
hello.c:21: error: (Each undeclared identifier is reported only once
hello.c:21: error: for each function it appears in.)
hello.c:21: error: syntax error before string constant
hello.c: At top level:
hello.c:25: error: syntax error before "void"
hello.c: In function `cleanup_module':
hello.c:27: error: `KERN_DEBUG' undeclared (first use in this function)
hello.c:27: error: syntax error before string constant
[yangww_cs@localhost driver]$

请问这个问题怎么解决啊?
很着急啊~
已经试了两天了,但是还是这样~
谢谢大家帮忙啊



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


我的QQ是347073999
大家可以加Q解决~
远程协作~
麻烦了啊~
实在头大啊

论坛徽章:
0
2 [报告]
发表于 2010-10-13 09:02 |只看该作者
首先 hello.c修改一下,头文件可以加下面几个,<linux/modversions.h>是老式的头文件了,新版kernel里面不在了, init_module/cleanup_module定义时不要使用 static,2.6里面通常使用module_init/module_exit来定义,这个时候可以加static,如果直接定义init_module/cleanup_module就不能加static。

/*
* Hello world module.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>

#if defined(CONFIG_SMP)
#define __SMP__
#endif


int __init init_module(void)
{
        printk(KERN_DEBUG "Hello, kernel!\n");
        return 0;
}

void __exit cleanup_module(void)
{
        printk(KERN_DEBUG "Good-bye, kernel!\n");
}

另外,最好写一个Makefile,简单的如下:

obj-m := hello.o

KDIR  := /lib/modules/$(shell uname -r)/build
PWD   := $(shell pwd)

default:
        $(MAKE) -C $(KDIR) M=$(PWD) modules

论坛徽章:
0
3 [报告]
发表于 2010-10-13 16:33 |只看该作者
回复 2# seaquester


    出现了./Makefile: line 1: obj-m: command not found
./Makefile: line 2: shell: command not found
./Makefile: line 2: KDIR: command not found
./Makefile: line 3: shell: command not found
./Makefile: line 3: PWD: command not found
: command not found default:
./Makefile: line 5: MAKE: command not found
./Makefile: line 5: KDIR: command not found
./Makefile: line 5: PWD: command not found
./Makefile: line 5: -C: command not found

论坛徽章:
0
4 [报告]
发表于 2010-10-13 16:35 |只看该作者
回复 2# seaquester


    [root@localhost driver]# make -f my
make: Warning: File `my' has modification time 3.5e+02 s in the future
make -C /lib/modules/2.6.9-67.ELsmp/build M=/mnt/hgfs/Linux/driver modules
make[1]: Entering directory `/usr/src/kernels/2.6.9-67.EL-smp-i686'
scripts/Makefile.build:13: /mnt/hgfs/Linux/driver/Makefile: 没有那个文件或目录
make[2]: *** 没有规则可以创建目标“/mnt/hgfs/Linux/driver/Makefile”。 停止。
make[1]: *** [_module_/mnt/hgfs/Linux/driver] 错误 2
make[1]: Leaving directory `/usr/src/kernels/2.6.9-67.EL-smp-i686'
make: *** [default] 错误 2

论坛徽章:
0
5 [报告]
发表于 2010-10-13 16:37 |只看该作者
如果用Make -f 就出现

[root@localhost driver]# make -f my
make: Warning: File `my' has modification time 3.5e+02 s in the future
make -C /lib/modules/2.6.9-67.ELsmp/build M=/mnt/hgfs/Linux/driver modules
make[1]: Entering directory `/usr/src/kernels/2.6.9-67.EL-smp-i686'
scripts/Makefile.build:13: /mnt/hgfs/Linux/driver/Makefile: 没有那个文件或目录
make[2]: *** 没有规则可以创建目标“/mnt/hgfs/Linux/driver/Makefile”。 停止。
make[1]: *** [_module_/mnt/hgfs/Linux/driver] 错误 2
make[1]: Leaving directory `/usr/src/kernels/2.6.9-67.EL-smp-i686'
make: *** [default] 错误 2
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP