Analysis and solution of "error conflicting types for function" compiling error in gcc compiling C program

Source: Internet
Author: User
Tags function definition

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.