编译出错,何解??谢谢!!
各位大哥,小弟为了学驱动,写了一个非常简单的程序,在编译的时候出现下面的错误,请问何解??感谢先!!1.为什么Include头文件的时候会出错,我需要修改这些头文件吗???
2.为什么Include的头文件要使用linux/***的,有一些写一般的程序是可以直接使用的啊,比如说fcntl.h,为什么现在要使用linux/fcntl.h ???
3.后面居然说file_operations没有write,read等域,这是为什么???
In file included from test.c:11:
/usr/include/linux/malloc.h:3:2: warning: #warning The Use of linux/malloc.h is
deprecated, use linux/slab.h
In file included from /usr/include/linux/fs.h:23,
from test.c:12:
/usr/include/linux/string.h:8:2: warning: #warning Using kernel header in userl
nd!
In file included from test.c:15:
/usr/include/linux/proc_fs.h:70: parse error before "atomic_t"
/usr/include/linux/proc_fs.h:73: parse error before '}' token
/usr/include/linux/proc_fs.h:203: parse error before "void"
In file included from /usr/include/linux/bitops.h:69,
from /usr/include/asm/system.h:7,
from test.c:17:
/usr/include/asm/bitops.h:327:2: warning: #warning This includefile is not avai
able on all architectures.
/usr/include/asm/bitops.h:328:2: warning: #warning Using kernel headers in user
pace: atomicity not guaranteed
test.c:21: warning: `struct file' declared inside parameter list
test.c:21: warning: `struct inode' declared inside parameter list
test.c: In function `open_test':
test.c:23: union has no member named `usecount'
test.c: At top level:
test.c:26: warning: `struct file' declared inside parameter list
test.c: At top level:
test.c:26: warning: `struct file' declared inside parameter list
test.c:26: warning: `struct inode' declared inside parameter list
test.c: In function `release_test':
test.c:28: union has no member named `usecount'
test.c: At top level:
test.c:31: warning: `struct file' declared inside parameter list
test.c:51: warning: `struct file' declared inside parameter list
test.c: In function `init_module':
test.c:60: `TestDrv_opt' undeclared (first use in this function)
test.c:60: (Each undeclared identifier is reported only once
test.c:60: for each function it appears in.)
test.c: At top level:
test.c:75: variable `TestDrv_opt' has initializer but incomplete type
test.c:75: `TestDrv_opt' used prior to declaration
test.c:76: unknown field `read' specified in initializer
test.c:76: warning: excess elements in struct initializer
test.c:76: warning: (near initialization for `TestDrv_opt')
test.c:77: unknown field `write' specified in initializer
test.c:77: warning: excess elements in struct initializer
test.c:77: warning: (near initialization for `TestDrv_opt')
test.c:78: unknown field `open' specified in initializer
test.c:78: warning: excess elements in struct initializer
test.c:78: warning: (near initialization for `TestDrv_opt')
test.c:78: warning: excess elements in struct initializer
test.c:78: warning: (near initialization for `TestDrv_opt')
test.c:79: unknown field `release' specified in initializer
test.c:79: warning: excess elements in struct initializer
test.c:79: warning: (near initialization for `TestDrv_opt')
test.c:75: storage size of `TestDrv_opt' isn't known
~
说明,未使用Makefile,好像也不用吧,直接使用
gcc -c test.c编译
/*************test.c*****************/
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h> /* printk() */
#include <linux/malloc.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/fcntl.h> /* O_ACCMODE */
unsigned int test_major = 0;
int open_test(struct inode * inode,struct file * lp_file)
{
MOD_INC_USE_COUNT;
return 0;
}
int release_test(struct inode * inode,struct file * lp_file)
}
int release_test(struct inode * inode,struct file * lp_file)
{
MOD_DEC_USE_COUNT;
return 0;
}
ssize_t read_test(struct file * lp_file,char * buf,size_t count,loff_t * f_pos)
{
int left;
int data = 1;
/*check if the buf is ok!!*/
/*
if(verify_area(VERIFY_WRITE,buf,count) == -EFAULT)
return -EFAULT;
*/
for(left=count;left>0;left--)
{
/*write data to user proc*/
if(copy_to_user(buf,&data,1))
{
return -EFAULT;
}
buf++;
}
buf++;
}
return count;
}
ssize_t write_test(struct file * lp_file,const char * buf,size_t count,loff_t *
_pos)
{
return count;
}
/*now implete the register module*/
int init_module(void)
{
int ret;
/*regist the drv*/
ret = register_chrdev(0,"test",&TestDrv_opt);
if(ret<0)
{
printk("<1>test:can't get major number.\n");
return ret;
}
/*update the test major*/
if(test_major == 0) test_major = ret;
return 0;
}
return 0;
}
void cleanup_module(void)
{
unregister_chrdev(test_major,"test");
}
/*define file operation,and then */
struct file_operations TestDrv_opt = {
read: read_test,
write:write_test,
open: open_test,
release:release_test,
};
/*
struct file_operations TestDrv_opt;
TestDrv_opt.read = read_test;
TestDrv_opt.write = write_test;
TestDrv_opt.open = open_test;
TestDrv_opt.write = write_test;
*/ 使用下面的方式编译,结果如下:何解??谢谢!!
[ 10:32:38 /prog/TestDrv ] # gcc -D__KERNEL__ -DMODULE -I/usr/src/linux-2.4/inc
ude -O -Wall test.c
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.text+0x18): In functi
n `_start':
../sysdeps/i386/elf/start.S:77: undefined reference to `main'
/tmp/cc6M3ecs.o(.text+0x5): In function `open_test':
: undefined reference to `__this_module'
/tmp/cc6M3ecs.o(.text+0xb): In function `open_test':
: undefined reference to `__this_module'
/tmp/cc6M3ecs.o(.text+0x1c): In function `release_test':
: undefined reference to `__this_module'
/tmp/cc6M3ecs.o(.text+0x22): In function `release_test':
: undefined reference to `__this_module'
/tmp/cc6M3ecs.o(.text+0x92): In function `init_module':
: undefined reference to `register_chrdev'
/tmp/cc6M3ecs.o(.text+0xa8): In function `init_module':
: undefined reference to `printk'
/tmp/cc6M3ecs.o(.text+0xda): In function `cleanup_module':
: undefined reference to `unregister_chrdev'
collect2: ld returned 1 exit status 使用下面方式编译,问题已解:
Display all 2082 possibilities? (y or n)
[ 10:47:29 /prog ] # gcc -D__KERNEL__ -DMODULE -I/usr/src/linux-2.4/include -c -O -Wall test.c -o test1.o
页:
[1]