【发布时间】:2012-02-17 06:33:19
【问题描述】:
这段代码只是我在实际代码中发现的一种情况,它非常大,所以我给出了这个。在这段代码中,结构“struct node”没有定义它是在另一个c源中定义的文件。
我的c源代码:
/* test.c */
1 #include<stdio.h>
2 #include "test2.h"
3
4 void f(struct node * k)
5 {
6
7 }
我的头文件:
/* test2.h */
1 extern void f(struct node * k);
当我用 gcc 编译这段代码来创建一个目标文件时:
gcc -w -c test.c
我明白了:
test.c:6: error: conflicting types for 'f'
test2.h:1: error: previous declaration of 'f' was here
我已经给出了函数f()的完整原型。为什么我会收到此错误?
另一件事是,当我在 test.c 中不包含头文件 test2.h 并在 test.c 中显式声明函数原型时,它会编译成功地。代码如下:
/* test.c */
1 #include<stdio.h>
2 void f(struct node *k);
3
4 void f(struct node * k)
5 {
6
7 }
gcc -c -w test.c
没有错误。
你能解释一下为什么这次我没有收到错误吗?
【问题讨论】:
-
您使用的是什么版本的 GCC?对于 4.6.1,我在您的最后一个示例中也出现错误(原型和定义在同一个文件中) - 有或没有
extern。
标签: c