Note that "precision" rather than "correctness" is used here. If the header file is not correctly included, the compiler will not generate the final target code. So what do I mean by using the word "exact? Contains two meanings.
The first layer means that only necessary header files are included. Figure 1 is a simple example program. Let's assume that this file only has the implementation of a function foo (), and there is only a simple row of print statements in the foo () function. We know that the prototype Declaration of the printf () function comes from the stdio of the standard library. h header file, foo. except stdio. h also contains another redundant header file-time. h. From the implementation perspective, there is no problem. However, when a project is large, this similar behavior will highlight the problem of low compilation efficiency. When foo. when a c file is compiled, the first step is preprocessing. The final result of preprocessing can be viewed as stdio. h and time. put all content in h to foo. c file, of course, if stdio. h and time. h contains other header files, and all of them will be put into the final foo. in the c file, Figure 2 shows the final file obtained by using the-E Option of GCC. c. It can be seen that a 7-row file has changed to 1419 rows. What does this mean? This means that compilation takes longer if the header file is contained. Because, when the compiler finally analyzes the lexical and syntax in the final. c file, it must process one line from start to end. Don't underestimate the amount of time that every file has to come out. When a project has hundreds of thousands of lines of code, the amount of time it takes cannot be underestimated. When this phenomenon becomes a problem that cannot be ignored, the effort required to eliminate it is already great, and even unacceptable to the project team. In addition, most such corrective actions are "physical labor ".
Foo. c
# Include <stdio. h>
# Include <time. h>
Void foo ()
{
Printf ("Just for an example! \ N ");
}
Figure 1
Yunli.blog.51cto.com ~
$ Gcc-E foo. c> final. c
Yunli.blog.51cto.com ~
$ Vi final. c
1414 #3 "foo. c" 2
1415
1416 void foo ()
1417 {
1418 printf ("Just for a test! \ N ");
1419}
: Set nu
Figure 2
The second layer means that try not to include other header files in the header file. Instead, try to include them in the. c source file. For the two implementation methods shown in figure 3 and figure 4, the light is exactly the same from the perspective of the foo () function, but for stdio. h. The header file contains one that is placed in foo. h, while the other is placed in foo. in c. In most cases, the purpose of designing a module is to be used by other modules, while other modules usually need to include the foo. h header file when using the foo () function. Using the implementation method shown in Figure 3 will result in the inclusion of foo. the h header file will indirectly include stdio. h. Then, the Compilation speed reduction mentioned above occurs, and the implementation of Figure 4 does not. Think about it. The printf () function only needs to be used in the implementation of the foo () function, rather than foo. the h header file knows this information, so there is no need to follow it in foo. the h header file contains stdio. h.
Foo. h
# Include <stdio. h>
Void foo ();
Foo. c
# Include "foo. h"
Void foo ()
{
Printf ("Just for an example! \ N ");
}
Figure 3
Foo. h
Void foo ();
Foo. c
# Include <stdio. h>
# Include "foo. h"
Void foo ()
{
Printf ("Just for an example! \ N ");
}
Figure 3
I believe that many readers have similar project experiences. To include header files, it is easy to define an all-encompassing public header file during project progress, then let other source program files only need to include this header file, thus eliminating the need to consider what to include and what not to include when creating a new source program. After reading this good programming habit, I believe readers will understand that it is not a good practice.
This article from "to Jane Li cloud" blog, please be sure to keep this source http://yunli.blog.51cto.com/831344/275313