Explain the relationship between the.h and.c documents in the C language project

  • 2020-05-19 05:15:34
  • OfStack

Explain the relationship between.h and.c documents in the C language project

In the days when compilers knew only the.c (.cpp) file, but not the.h file, people wrote a lot of.c (.cpp) files. Gradually, people found that many of the statements in the.c (.cpp) files were the same, but they had to type them repeatedly into every.c (.cpp) file, word for word. But even scarier is the need to check all.c (.cpp) files when one of the statements changes.

So people extract the duplicate, put it in a new file, and type #include XXXX into the desired.c (.cpp) file. That way, even if a declaration changes, you don't have to look around for it. Because this new file is often placed at the head of a.c (.cpp) file, it is called a "header file" with the extension.h.

At the beginning of our language learning stage, our program usually only has 1.c file or a few of these files, so we seldom encounter the headache of header file organization. As our program grows, the amount of code reaches thousands or even tens of thousands of lines, and the number of files grows. At this point, the organization of these files becomes a problem. In fact, the organization of these files is theoretically a problem of module design in software engineering and so on.

A brief description of what the header file does:

(1) call the library function through the header file. In many cases, the source code is not readily available (or allowed) to the user, as long as the header file and the base 2 library are provided to the user. The user only needs to invoke the library functionality according to the interface declaration in the header file, without caring how the interface is implemented. The compiler extracts the code from the library.
(2) header files can enhance type safety checks. If an interface is implemented or used in a way that is not identical to the declaration in the header file, the compiler will point out the error, and this simple rule can greatly reduce the burden of debugging and error correction on the programmer.

Let's say I define the declaration of a function in aaa.h, and then I create aaa.c in the same directory as aaa.h, aaa.c, and then I define the implementation of this function in #include in main, aaa.h, and then I can use this function. At run time, main will find the aaa.c file that defines this function. This is because: main function is the standard C/C++ program entry, the compiler will first find the file where the function is located. Assume compiler compiler myproj. c (including main ()), found it include mylib. h (including statement function void test ()), then the compiler will according to the predetermined path (Include path list and code file path) to the location of the search with the implementation of the same name of the file (extension. cpp or c, this case to mylib c), if it finds the file, Find the implementation code for this function (void test() in this case), and continue to compile. If the specified directory can not find the implementation file, or in the file and the subsequent each include implementation code is not found, it returns a compiler error. Actually include can "as" is the process of splicing process of a file, write statement and implementation respectively in the header file and C file, or will be written in the header file at the same time, the two theoretically there is no essential difference.

Theoretically C file with the content in the header file, as long as it is C language support, no matter what can write, you write the body of the function in the header file, for example, as long as in any one C file contains the header file can be compiled into the function of the target file 1 part (is to compile a C file, if not any C file contains the header files, this code is useless), you can undertake the function declaration in C files, variable declarations, structure, and it is not a problem!!!!!! So why does 1 have to be split into header files and C files? And why are functions, variables, macros, and structs all declared in the header? And in the C file to carry out the variable definition, function implementation??

To understand how C files differ from header files, you first need to understand how the compiler works. In general, the compiler does the following:

1. Pretreatment stage
2. Morphology and grammar analysis stage
3. In the compilation stage, it is first compiled into pure assembly statement, and then compiled into the base 2 code related to CPU to generate each target file
4. Connection phase, each target file absolute address location and paragraphs in the code, generate executable file associated with a particular platform, the compiler is at compile time C file for, that is to say, if one of your project C file all have no, so your project will not be able to compile, connector on the target file for the unit, it will be one or more target file variable function with the relocation, to produce the final executable file, application development on PC, 1 have 1 main function, this is all the terms of the compiler. In order to generate a final executable, you need some object files, C files, which in turn require an main function as the entry point to the executable.

To put it simply, the compilation of C is divided into preprocessing, compilation, assembly and linking > test.i = > test.s = > test.o = > test) four major phases. In the macro processing of #include in c file, all contents of h file referenced in c will be written to c file in the pre-processing stage, and finally, the intermediate i file will be generated. At this time, the contents of h file will be written to c file. It also provides a channel, the reuse of code with many c file can be referenced to a h file, so the h file will be put into multiple c file is compiled many times, this also is h file cannot put definition can only put the cause of the statement, released definition is compiled many times, at the time of application link (system defines multiple int a; The declaration means that the extension of the definition will end up at one definition, so there will be no repeated definition error in link.

In programming, we must have used 1 in h files


#ifndef  XXX_H
#define  XXX_H
 // ... 
#endif

Well, what's the use of it? Eliminating duplicate definitions when the h file references each other. Of course, the macro definition plays a role in the pre-processing phase, and there is no macro in the compilation process.


A.h
int a();
 
B.h
#include "A.h"
 
C.h
#include "A.h"
 
D.h
#include "A.h"
#include "B.h"

In the D.h file above, two int a() are repeated; This is a bit repetitive, where conditional compilation macros come in handy


A.h
#ifndef A_H
#define A_H
int a();
#endif

So you don't have to repeat the definition.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: