When you use gcc to compile a C program, you may encounter a compilation error of "error: conflicting types for 'function. Literally, the definition and declaration of a function are inconsistent. In this article, I will make a simple analysis of this error (the gcc version used is 4.9.0 ).
(1)First, let's look at an example where the definition and declaration of a function are inconsistent:
# Include <stdio. h>
Int func (int );
Int func (void ){
Return 0;
}
Int main (void ){
Func ();
Return 0;
}
Compile the program:
Gcc-g-o 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 );
It can be seen that the declaration and definition of "func" are inconsistent (one has a parameter and the other does not), so this error occurs during compilation.
(2)Recently, I have been porting an old program from Solaris to Linux. This error also occurs during compilation. However, I found that the declaration of the function in the header file is exactly the same as that of the function definition, which makes me very strange. After checking for nearly one day, the final conclusion is that the function parameter type is defined after the function declaration. The simplified code is as follows:
# Include <stdio. h>
Void func (struct A * );
Struct {
Int;
};
Void func (struct A *)
{
Printf ("% d", A-> );
}
Int main (void ){
// Your code goes here
Struct A = {4 };
Func (& );
Return 0;
}
The definition of "structure A" is placed after the declaration of the "func" function, and the parameter of the func function is of the "structure A *" type. The compilation result is as follows:
Gcc-g-o a. c
A. c: 3: 18: warning: 'struct a' declared inside parameter list
Void func (struct 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. c: 3: 6: note: previous declaration of 'func' was here
Void func (struct A * );
^
We can see that the compilation error "error: conflicting types for 'func'" is also Output. Maybe you can give a warning.
I checked the Makefile of the program and turned off all the compilation warnings. Maybe there are too many compilation warnings.