c source files to be included in your header file

Original blog link:

https://blog.csdn.net/khwkhwkhw/article/details/49798985?utm_source=app&from=timeline

 

 

introduction:

            We often found in c project, the source file to include your own header file. All along, I do not know why. now I know.

Previous knowledge:

 I think, .c file does not need to include your own .h file. .h file contains declarations of functions and global variables defined in the .c file, .h file is the external interface file .c file provided. Since .h file is the external interface file .c file provided, so no need to .c file contains its own .h files (.h files are provided with the external, internal and why should it be included again).

          Given this understanding, I do not understand for the project .c source file contains its own .h header files, I do not know why.

Now this knowledge:

          But now, I want to know why the source file contains its own header files.

Below, a section in the book's words:

"If you want the compiler can check that declaration, must take into global declarations in header files in particular, never put external function prototypes (that is, the function declaration) in a .c file: it is usually defined consistency can not be checked, and contradictory prototype (that is, the function declaration) is worse than useless. "

Note: The external function of the prototype, it is to declare an external function.

Understanding of this passage:

      Why: "Never put a prototype external function in a .c file"

The external function A refers to a function defined outside the file Bc, Bc file using an external function A, you need to declare an external function A (declaration of the external function is an external function prototypes). A statement to this external function, the file can not be placed inside Bc to achieve.

By way of example:

① If the project has two source files ac and bc; ac header file ah, bc header files for the bh.

②a.c defines a function sum.

③b.c To refer to this sum function. Approach is: declare sum this function in bc. Bc then you can use the sum function.

This approach is to declare external functions into the sum of bc in the past. However, such an approach is wrong.

Wrong reasons :

sum is defined in the ac, and the sound is clear in bc, the sum function definitions and declarations are not in the same file. Definitions and declarations are not the same file, compile time, the compiler will not be able to definitions and declarations of conformity to be checked. Thus, if the sum of inconsistent definitions and declarations, the compiler can not check it out (definitions and declarations are not the same file), it will not compile time error, but program runs could be wrong. And such a mistake, it is not very easy to find.

In view of this, just say: "Never put a prototype external function in a .c file."

How can that make the compiler checks the consistency of definitions and declarations it?

I said earlier, if the prototype external function in a .c file, the compiler can not check the consistency of definitions and declarations (declarations and definitions are not the same file). So, let the compiler checks the consistency of definitions and declarations it is naturally the definitions and declarations in the same file, and how to define and declare in the same document it?

Answer: function source file defined in the source file corresponding header file declaration, then the source file contains its own header file. Such definitions and declarations would be placed in the same file.

 

 

 

Cited above example: ac function sum defined, and the function is essentially external sum function can be called other source files. So, we declare sum function in place ah. Then ac source file also contains its own header file, that is, ah file. The bc file you want to reference sum function, it can be included directly ah file.

Defined sum in the ac functions, the statement is in the ah, but due to ac contains ah, so definitions and declarations sum is in the same file in the ac. In this way, when the compiler can do to check the consistency of the sum function definitions and declarations, and if not, it will error.

As for the other source files reference the external function sum, not by way of direct statement, but rather by the way ah header file contains.

In this way, the compiler checks the consistency of the sum function definitions and declarations are not being given, it indicates that the sum function declaration defines the ac and ah the sum function is the same. By then the other source files are included directly ah, to use the function sum, it also ensures the consistency of the sum of the function declarations and definitions.

in conclusion

c source files you want to include your own header file, the purpose is to allow the compiler to check the consistency of definitions and declarations.

Published 29 original articles · won praise 3 · Views 3381

Guess you like

Origin blog.csdn.net/qq_41601836/article/details/105122424