Detailed explanation of header files and source files in C++
1. C++ compilation mode
Generally, in a C++ program, there are only two types of files - .cpp file and .h file. Among them, the .cpp file is called the C++ source file, and the source code of C++ is placed in it; while the .h file is called the C++ header file, and the source code of C++ is placed in it.
The C++ language supports "separate compilation". In other words, all the contents of a program can be divided into different parts and placed in different .cpp files. The things in the .cpp file are relatively independent. When compiling (compile), you do not need to communicate with other files. You only need to link with other target files after compiling it into the target file. For example, a global function "void a() {}" is defined in the file a.cpp, and this function needs to be called in the file b.cpp. Even so, the files a.cpp and b.cpp do not need to know each other's existence, but can compile them separately. After compiling them into the target file, link them, and the entire program can be run.
How is this achieved? From the perspective of writing programs, it is very simple. In the file b.cpp, before calling the "void a()" function, declare the function "void a();" first. This is because the compiler will generate a symbol table when compiling b.cpp. Symbols like "void a()" that cannot be seen will be stored in this table. When linking again, the compiler will look for the definition of this symbol in other object files. Once found, the program can be generated smoothly.
Note that there are two concepts mentioned here, one is "definition" and the other is "declaration". Simply put, "definition" means describing a symbol in a complete and complete way: whether it is a variable or a function, what type it returns, what parameters it needs, etc. "Declaration" just declares the existence of this symbol, that is, tells the compiler that this symbol is defined in other files. I will use it first. When you link, go to another place to find out what it is. When defining, you must define a symbol (variable or function) completely according to C++ syntax, and when declaring, you only need to write the prototype of this symbol. It should be noted that a symbol can be declared multiple times throughout the program, but must be defined only once. Just imagine, if there are two different definitions of a symbol, who should the compiler listen to?
This mechanism brings many benefits to C++ programmers, and also leads to a method of writing programs. Consider that if there is a very commonly used function "void f() {}" that will be called in many .cpp files in the entire program, then we only need to define this function in one file and declare this function in other files. A function is easy to deal with, and it only means one sentence to declare it. However, what if there are too many functions, such as a bunch of mathematical functions, there are hundreds of them? Can every programmer be sure to accurately write down and write down all functions in the form of them?
2. What is a header file
Obviously, the answer is impossible. But there is a very simple way to help programmers save the trouble of remembering so many function prototypes: we can write all the declaration statements of hundreds of functions first and put them in a file. When the programmer needs them, copy all these things into his source code.