在使用gcc编译C程序时,有时会碰到“error: conflicting types for ‘function’”的编译错误。从字面意义上理解,是说函数的定义和声明不一致。
(一)首先我们看一个函数的定义和声明不一致的例子:
#include <stdio.h>int func(int a);
int func(void) {
return0;
}
int main(void) {
func();
return0;
}
编译程序:
gcc -g -o a a.c
a.c:5:5: error: conflicting types for ‘func’
int func(void) {
^
a.c:3:5: note: previous declaration of ‘func’ was here
int func(int a);
#include <stdio.h>void func(struct A *A);
struct A {
int a;
};
void func(struct A *A)
{
printf("%d", A->a);
}
int main(void) {
// your code goes herestruct A a = {4};
func(&a);
return0;
}
gcc -g -o a a.c
a.c:3:18: warning: ‘struct A’ declared inside parameter list
void func(struct A *A);
^
a.c:3:18: warning: its scope is only this definition or declaration, which is probably not what you want
a.c:9:6: error: conflicting types for ‘func’
void func(struct A *A)
^
a.c:3:6: note: previous declaration of ‘func’ was here
void func(struct A *A);
^
可以看到也输出了“error: conflicting types for ‘func’”的编译错误,也许编译警告可以给一点提示吧。