Due to the development of VC + +, we need to master some of the original rational things. In particular, the concept of source and header files requires in-depth understanding of it:
about the difference between header and source files
First, we can put everything in a. cpp file.
The compiler then compiles this. cpp into. Obj,obj What is it?
is the compile unit. A program can consist of a compilation unit or multiple compilation units. If you don't want your source code to be hard to read,
Please use more than one compilation unit. (a function cannot be placed in two compilation units, but more than two
Can be placed in a unit, that is, CPP inside)
Then it's a. cpp corresponds to an. obj and then links all of the obj (through a program called a linker) to form an. exe, which is the program.
What if a. cpp uses another. cpp-defined function? Just write his function declaration on this. cpp. The rest of the work is done by the linker, and you can call the function casually.
The linker joins all of the obj, but what if it happens to have the same function or external variable? How does he identify?
In general, you cannot allow two of the same function names or external variable names to appear in the same program.
But thankfully, C + + can be defined by a keyword called a link attribute, which is a function of the entire program.
Common, or just used in a compilation unit obj.
These keywords are extern and static; extern is the meaning of an external link, that is, in addition to this unit, the outer element
is also able to access this function. Static is an internal link that belongs to its own unit.
Said for so long, has not said. H's role?
Not really. h can also work well, but when you find an externally linked function or external variable, you need a lot of declarations, because the C + + language, when using functions and variables, must be declared, why should he declare? In order to find a better part of the specification. You don't have to be paranoid. A compilation unit automatically obtains information from another compilation unit, knowing how you define the function.
So, as long as you use the unit of the function, you have to write a statement in that. cpp, this is not very troublesome, and, if you want to modify, you have to change one. This is so unbearable.
H was born to solve this problem, and he contained these public things. Then all the. cpp that needs to use this function is included in the #include. You need to change it later, and just modify a content.
Please be careful not to abuse. H,.h don't write code,. h is not. cpp warehouse, everything is stuffed inside.
If you write code inside, when other. cpp contains him, there will be a duplication of definitions, such as the function func () {printf}, the header file a.h, there are some a.cpp required statements, etc.
Then you find that B.cpp needs to use a function inside a.cpp, and it's nice to include a.h in it.
Note that, #include并不是什么申请指令, he is the contents of the specified file, the copy is intact.
This is actually the definition of a Func () function in both A.cpp and B.cpp.
If this function is the internal link static, fortunately, a waste of space;
If it is extern, external links (this is the default), then under the same program can not appear
A function with the same name requires that the connector will mercilessly give you a connection error!