---------
How to use H files and C files? In general, the H file is a Declare, which is define in the C file. Because the C file should be compiled into the library file (Windows is .Obj / .lib, UNIX is .o / .a), if others want to use your function, then reference your H file, so h file Generally, variables, macro definitions, enumeration, structures, and function interfaces, just like an interface description file. The C file is a detail.
The maximum use of H files and C files is declaration and implementation. This feature should be recognized, but I still see some people like to write the function in the H file, this habit is very bad. (If it is C , for its template function, only the implementation and declarations in the VC are written in a file because the VC does not support the export keyword). Moreover, if the implementation of the function is written in the H file, you have to add the dependency of the header file in makefile, which will make your makefile unregistered.
Finally, there is a place that is most important to pay attention to: the global variable with initialization should not be put in the H file!
For example, there is a structure that handles error messages:
Char * errmsg [] = {
/ * 0 * / "no error",
/ * 1 * / "open file error",
/ * 2 * / "failed in sending / receiving a message,
/ * 3 * / "Bad Arguments",
/ * 4 * / "Memeroy Is Not Enough",
/ * 5 * / "service is down; try lat),
/ * 6 * / "unknow information",
/ * 7 * / "a socket Operation Has Failed",
/ * 8 * / "permission Denied",
/ * 9 * / "Bad Configuration File Format",
/ * 10 * / "Communication Time Out",
......
......
}
Please don't put this thing in the header file, because if your header is used by 5 function libraries (.lib or .a), he is linked in these 5 .lib or .a, And if your program uses a function in these five functions, and these functions have been used in an array of error information. Then this information will have 5 copies existing in your executter. If your Errmsg is big, and your execution file will become bigger.
The correct way to write it into the C file, and then add external declaration on each of the Errmsg's C file headers, let the compiler will take him when the compiler is in the link. Come, there will only be an errmsg exists in the execution file, and so that this is good to package.
The most crazy thing I have encountered is that in my target file, this errmsg has a total of 112 copies, and the execution file is about 8m. When I put errmsg in the C file, and after more than a thousand C files with Extern declaration, all the function library files decreased by about 20%, and my executive file was only 5m. There is a little less than 3m. [Note]
-----
Some friends said to me, this is just a special case, because if errmsg exists multiple copies in the execution file, speed up the program running speed, the reason is that the multiple copies of Errmsg will reduce the system's memory change, achieve efficiency Increase. There is only one errmsg we mentioned here. When a function is to use Errmsg, if the memory is far apart, it will generate a change, but the efficiency is not high.
This statement is not reasonable, but in general, for a relatively large system, errmsg is relatively large, so that the copy caused the size of the execution file to become large, not only adding the system loading time, but also a program in memory More page. For ERRMSG such data, in general, it will not be used frequently when the system is running, so the memory switching is not frequent. Under the trade-off, there is only one efficiency of Errmsg. Even if it is used frequently like LogMSG, the operating system's memory scheduled algorithm will allow such frequently used pages to be stored, so there will be no memory change issues.