header files and source files
Because the header file is intended for reuse, in a complex program, the header file may be implicitly duplicated. If the header files are all function declarations, that's not a big problem. If there is a function definition in the header file (such as program 11.2), then there will be an error that the function is repeatedly defined, and the program will not run. We can use the function declaration and the way of defining separation: put all the declarations in the shape.h, and put all the definitions in the Shape.cpp. Note You must include shape.h in Shape.cpp, or an error will occur when you compile the connection. We still include shape.h when we use it, but since the definition of the function is not in the header file, it is not duplicated.
Detail # include
We use the # include command almost every time we write a program, so what does this command mean?
#include是一条编译预处理命令. What do you mean by the compile preprocessing command? We know that every sentence in the program can be represented at run time. For example, a variable or function declaration creates a variable or function, and the output statement prints the characters on the screen. However, the compile preprocessing command is not reflected at run time because it is a message to the compiler, not a statement that needs to be executed in the program. The compile Pre-processing command is not just an # include, in C + +, all commands that begin with # are compiled preprocessing commands such as # If, #else, #endif, #ifdef, #ifndef, #undef和 # define, and so on.
When the compiler encounters the # include command, it inserts the file from the command into the current file. It is not difficult to imagine that the main.cpp file of program 11.2 essentially contains all the statements in the Shape.h file. So it can call each function in the shape.h file smoothly.
Source: http://c.biancheng.net/cpp/biancheng/view/65.html
question: h header file and C file what should I put in?
Describe:
I used to define the function in the. h header file, and then put the. h file into the. c file where main () is located, and call the. h function in the Mian () function.
Recently a friend told me not to do this, you should write the function declaration in. h, and create a. c file to define the specific function operation, and then put the. h file into the. c file where main () is called.
Do not know how to deal with this problem is good?
Solution 1:
The function declaration is written in the. h, and another. c file defines the specific function operation, and then the. h file include to the. c file where main ()
Solution 2:
Recently a friend told me not to do this, you should write the function declaration in. h, and create a. c file to define the specific function operation, and then put the. h file into the. c file where main () is called.
It is generally stated in. h that the. cpp in the corresponding name writes the interface with the function (except the template), and then the other. CPPX needs to be called to include the header file.
Solution 3:
A good habit, in addition to the Static,inline,extern function, the normal function should not be implemented in. h, just put the declaration in H, implementation put on. In C.
Solution 4:
Generally recommended header files are used for declarative, source files for definition and implementation, but some definitions can also be placed in header files.
cite the Landlord zhouzb889 post:
I used to define the function in the. h header file, and then put the. h file into the. c file where main () is located, and call the. h function in the Mian () function.
Recently a friend told me not to do this, you should write the function declaration in. h, and create a. c file to define the specific function operation, and then put the. h file into the. c file where main () is called.
Do not know how to deal with this problem is good?
Solution 5:
Your friend is right.
The key to this problem is that your approach is suitable for small projects where there is no case of having a header file multiple times, or it is easy to avoid this situation, if the code of the project is more,
Your approach will have a bigger problem.
Multiple files will appear with one at a time. h file, and then the compilation process will appear redefined, then you have to Chi Chi the code bar ...
Solution 6:
Can include macro definitions, class definitions, struct definitions, template definitions, global variable declarations, function declarations, inline function definitions, and so on.
Do not include global variable definitions, function definitions.
The above examples may not be exhaustive.
In essence, you just have to understand one thing: the header file is used by others, so the same header file is most likely to be included in more than two CPP files (e.g. <iostream> is often included in multiple CPP files). So anything that repeats two times in two compilation units is not put in. h.
Links: http://www.codes51.com/itwd/2908358.html
What does a header file in C + + usually contain?