这段代码只是我在实际代码中发现的一个情况,因为我的实际代码非常大,所以我提供了这个例子。在这段代码中,结构体“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
I get:
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
无错误。
请问为什么这一次我没有收到错误提示?