The difference between .h and .cpp files

Respectively on header and source files

First of all, we can put everything in one .cpp file.

Then the compiler will compile the .cpp to .obj, obj is what?

Is a compilation unit. A program that consists of a compilation units,

Can also have multiple compilation units. If you do not want your source code becomes difficult to read the words,

A plurality of coding units to use it. (A function which can not be put two compilation units, but two or more

A unit can be placed, respectively, i.e. inside the cpp)

    It is a .cpp corresponding to a .obj, then all the obj linked (through a program called the linker),

Form a .exe, which is a program.

    If you use a function to another .cpp .cpp defined how to do? Just write in the .cpp planted his function declaration

On it. The remaining work done by the linker to help you, you can easily call the function.

    Link will connect all of obj, but if you happen to have the same function or external variable is how to do? How to identify him?

Is generally not allowed in the same program, the same two external function name or variable name appears.

    But only Fortunately, c ++ can be defined by the link attribute called keywords, you belong to this function is the entire program

Public, or just a compilation unit obj inside use.

    The keyword is extern and static; extern external links mean, that is in addition to this unit, an external unit

You can also access this function .static internal links from their own unit.

Having said so much, yet say .h's role in it?

    In fact, there is no .h can work well, but when you find a link to an external function or external variable, requires a lot of part

After the statement, because c ++ language, when using functions and variables, you must declare him, why should declare? Statement was

He knew the specifications, in order to better discover and specifications are not part of. You do not imagine a compilation unit, automatically from another

Compilation units get what information to know how do you define this function.

    So, as long as the unit to use this function, you must write a statement in the .cpp inside, this is not too much trouble,

And, if you want to modify, you must modify one by one. This is really unbearable.


.h is to solve this problem was born, he included these public things. Then all .cpp need to use this function, you need only

#Include the can go in. After the need to amend, modify only a piece of content.


Please be careful not to abuse .h, .h which do not write code, .h not .cpp warehouses, anything stuffed inside.

If you write code in it, when other .cpp contain him, there will be a repetitive defined,

For example the function func () {printf}; header file into ah, there is also some statements like a.cpp required;

Then you need to use a function find b.cpp a.cpp inside, very happy ah will be included.

Note that the application instruction # include what is not, he is the contents of the specified file, copy intact

Come in.


This time actually a.cpp and b.cpp have a defined function func ().

If this function is a static internal links, then, fortunately, has doubled wasted space;

If extern, external links (this is the default), then according to not appear in the same program

Required function of the same name, the connector will mercilessly give you a connection error!

 

Reproduced in: https: //www.cnblogs.com/shelvenn/archive/2008/02/02/1062446.html

Guess you like

Origin blog.csdn.net/weixin_34247155/article/details/93272929