LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1341|回复: 6

编译一个最简单的驱动程序,出现了太多错误,不知所措,求救!

[复制链接]
发表于 2005-7-15 15:10:48 | 显示全部楼层 |阅读模式
在一本书上原抄了一个类似helloworld的驱动程序,TestDrive.c
shell下执行以下命令进行编译: gcc -O2 -DMODULE -D__KERNEL__ -c TestDriver.C
出现了这么多错误(比较长,请耐心点帮我看看,谢谢)

*********************以下是错误信息**************************
TestDriver.C:4:27: linux/modules.h: 没有那个文件或目录
In file included from /usr/include/linux/fs.h:18,
                 from TestDriver.C:9:
/usr/include/linux/list.h:37: parse error before `new'
/usr/include/linux/list.h: In function `void __list_add(...)':
/usr/include/linux/list.h:39: `next' undeclared (first use this function)
/usr/include/linux/list.h:39: (Each undeclared identifier is reported only once
   for each function it appears in.)
/usr/include/linux/list.h:39: parse error before `;' token
/usr/include/linux/list.h:42: `prev' undeclared (first use this function)
/usr/include/linux/list.h:42: parse error before `;' token
/usr/include/linux/list.h: At global scope:
/usr/include/linux/list.h:53: parse error before `new'
/usr/include/linux/list.h: In function `void list_add(...)':
/usr/include/linux/list.h:55: parse error before `,' token
/usr/include/linux/list.h: At global scope:
/usr/include/linux/list.h:66: parse error before `new'
/usr/include/linux/list.h: In function `void list_add_tail(...)':
/usr/include/linux/list.h:68: parse error before `,' token
In file included from /usr/include/linux/fs.h:23,
                 from TestDriver.C:9:
/usr/include/linux/string.h:8:2: warning: #warning Using kernel header in userland!
In file included from /usr/include/linux/sched.h:14,
                 from /usr/include/linux/mm.h:4,
                 from TestDriver.C:10:
/usr/include/linux/timex.h: At global scope:
/usr/include/linux/timex.h:173: field `time' has incomplete type
In file included from /usr/include/linux/bitops.h:69,
                 from /usr/include/asm/system.h:7,
                 from /usr/include/linux/sched.h:16,
                 from /usr/include/linux/mm.h:4,
                 from TestDriver.C:10:
/usr/include/asm/bitops.h:327:2: warning: #warning This includefile is not available on all architectures.
/usr/include/asm/bitops.h:328:2: warning: #warning Using kernel headers in userspace: atomicity not guaranteed
In file included from /usr/include/linux/signal.h:4,
                 from /usr/include/linux/sched.h:25,
                 from /usr/include/linux/mm.h:4,
                 from TestDriver.C:10:
/usr/include/asm/signal.h:107: 'sigset_t' is used as a type, but is not defined
   as a type.
In file included from /usr/include/linux/sched.h:81,
                 from /usr/include/linux/mm.h:4,
                 from TestDriver.C:10:
/usr/include/linux/timer.h:45: 'spinlock_t' is used as a type, but is not
   defined as a type.
TestDriver.C:18: variable `file_operations tdd_fops' has initializer but
   incomplete type
TestDriver.C:41: `BUF_LEN' was not declared in this scope
TestDriver.C: In function `int device_open(inode*, file*)':
TestDriver.C:53: `MOD_INC_USE_COUNT' undeclared (first use this function)
TestDriver.C: In function `void device_release(inode*, file*)':
TestDriver.C:65: `MOD_DEC_USE_COUNT' undeclared (first use this function)
TestDriver.C: In function `int device_read(inode*, file*, char*, int)':
TestDriver.C:71: `VERIFY_WRITE' undeclared (first use this function)
TestDriver.C:71: `verify_area' undeclared (first use this function)
TestDriver.C:75: `_put_user' undeclared (first use this function)
TestDriver.C: In function `int init_module()':
TestDriver.C:86: `register_chrdev' undeclared (first use this function)
TestDriver.C:88: `KERN_INFO' undeclared (first use this function)
TestDriver.C:88: `printk' undeclared (first use this function)
TestDriver.C: In function `void cleanup_module()':
TestDriver.C:101: `unregister_chrdev' undeclared (first use this function)
TestDriver.C: At global scope:
TestDriver.C:18: storage size of `tdd_fops' isn't known
***************************************************************
我的头文件是这样的:

****************以下是TestDriver.c程序的开头部分**********************
/*TestDriver.c*/
/*一个简单的字符设备驱动实例*/
#define __NO_VERSION__
#include <linux/modules.h>
#include <linux/version.h>
char kernel_version [] = UTS_RELEASE;
#define KERNEL
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>
****************************************************************

从出错信息中可以看到,/usr/include/linux/下面有很多.h文件都有错误,可是那都是系统自带的阿,我绝对没有改过,我装的是redhat9.0系统

后面还有一些出错信息比如printk/register_chrdev/MOD_INC_USE_COUNT........没有定义,但我看了很多
驱动的例子,也特意包含了他们的所有头文件阿,

这只是一个最简单的例子,就有这么多问题,我要完成编写一个芯片可用驱动的任务痛苦+没有信心中...

谁能帮我指点一下,到底该怎么解决这么多错误?

谢谢!
发表于 2005-7-15 17:50:02 | 显示全部楼层
应该用源码所提供的头文件
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-7-15 21:40:50 | 显示全部楼层
“源码提供的头文件”是什么意思?
回复 支持 反对

使用道具 举报

发表于 2005-7-15 21:44:11 | 显示全部楼层
呵呵,你没有装Kernel的源代码~
把内核源代码装上就OK
回复 支持 反对

使用道具 举报

发表于 2005-7-16 10:59:19 | 显示全部楼层
gcc -O2 -DMODULE -D__KERNEL__ -I/usr/src/linux-2.4.20-8/include -c TestDriver.C
上面的/usr/src/linux-2.4.20-8/include是内核源代码(RH9.0)的头文件路径.
回复 支持 反对

使用道具 举报

发表于 2005-7-16 13:07:43 | 显示全部楼层
书上的例子好像是2.4的,你的kernel可能是2.6的。
2.4到2.6 内核的变化很大,编译通不过也很正常。
回复 支持 反对

使用道具 举报

发表于 2005-11-22 21:58:19 | 显示全部楼层
我也遇到了这个问题!
makefile文件如下(2.4.21-4是我的内核版本linux AS3):
SHELL = /bin/sh
CC = gcc
CXXFLAGS = -Wall -DMODULE -D__KERNEL__ -DLINUX -I/usr/src/linux-2.4.21-4.EL/include
test.o:test.c
        $(CC) $(MODCFLAGS) -c test.c


[root@lhcd-server dev_driver]# make
gcc  -c test.c

test.c:5:25: asm/uaccess.h: 没有那个文件或目录   
        //这里asm在我的电脑上是到asm-i386的符号连接,里面是有uaccessh的,怎么出错呢?
In file included from /usr/include/linux/sched.h:14,
                 from /usr/include/linux/mm.h:4,
                 from test.c:8:
/usr/include/linux/timex.h:173: field `time' has incomplete type
In file included from /usr/include/linux/bitops.h:69,
                 from /usr/include/asm/system.h:7,
                 from /usr/include/linux/sched.h:16,
                 from /usr/include/linux/mm.h:4,
                 from test.c:8:
/usr/include/asm/bitops.h:327:2: warning: #warning This includefile is not available on all architectures.
/usr/include/asm/bitops.h:328:2: warning: #warning Using kernel headers in userspace: atomicity not guaranteed
In file included from /usr/include/linux/signal.h:4,
                 from /usr/include/linux/sched.h:25,
                 from /usr/include/linux/mm.h:4,
                 from test.c:8:
/usr/include/asm/signal.h:107: syntax error before "sigset_t"
/usr/include/asm/signal.h:110: syntax error before '}' token
In file included from /usr/include/linux/sched.h:81,
                 from /usr/include/linux/mm.h:4,
                 from test.c:8:
/usr/include/linux/timer.h:32: field `vec' has incomplete type
/usr/include/linux/timer.h:37: field `vec' has incomplete type
/usr/include/linux/timer.h:45: syntax error before "spinlock_t"
/usr/include/linux/timer.h:53: syntax error before '}' token
/usr/include/linux/timer.h:63: field `list' has incomplete type
/usr/include/linux/timer.h:67: syntax error before "tvec_base_t"
/usr/include/linux/timer.h:101: syntax error before "tvec_bases"
/usr/include/linux/timer.h: In function `init_timer':
/usr/include/linux/timer.h:105: dereferencing pointer to incomplete type
/usr/include/linux/timer.h:105: dereferencing pointer to incomplete type
/usr/include/linux/timer.h:106: dereferencing pointer to incomplete type
/usr/include/linux/timer.h: In function `timer_pending':
/usr/include/linux/timer.h:121: dereferencing pointer to incomplete type
test.c: At top level:
test.c:13: warning: `struct file' declared inside parameter list
test.c:13: warning: its scope is only this definition or declaration, which is probably not what you want
test.c: In function `read_test':
test.c:17: `VERIFY_WRITE' undeclared (first use in this function)
test.c:17: (Each undeclared identifier is reported only once
test.c:17: for each function it appears in.)
test.c: At top level:
test.c:30: warning: `struct file' declared inside parameter list
test.c:35: warning: `struct file' declared inside parameter list
test.c:35: warning: `struct inode' declared inside parameter list
test.c:42: warning: `struct file' declared inside parameter list
test.c:42: warning: `struct inode' declared inside parameter list
test.c:47: variable `test_fops' has initializer but incomplete type
test.c:48: unknown field `read' specified in initializer
test.c:48: warning: excess elements in struct initializer
test.c:48: warning: (near initialization for `test_fops')
test.c:49: unknown field `write' specified in initializer
test.c:49: warning: excess elements in struct initializer
test.c:49: warning: (near initialization for `test_fops')
test.c:50: unknown field `open' specified in initializer
test.c:50: warning: excess elements in struct initializer
test.c:50: warning: (near initialization for `test_fops')
test.c:51: unknown field `release' specified in initializer
test.c:52: warning: excess elements in struct initializer
test.c:52: warning: (near initialization for `test_fops')
test.c: In function `init_module':
test.c:59: `KERN_INFO' undeclared (first use in this function)
test.c:59: syntax error before string constant
test.c: At top level:
test.c:47: storage size of `test_fops' isn't known
make: *** [test.o] Error 1
[root@lhcd-server dev_driver]#

大家帮我看看,谢谢拉,都是些什么原因引起的啊, 谢谢各位了!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表