The role of C + + *.h and *.cpp in compiling

Source: Internet
Author: User

First of all, we can put everything in a . cpp file. The compiler then compiles the. cpp into a . Obj,obj is the compilation unit .

A program can consist of a compilation unit or multiple compilation units . If you don't want your source code to be difficult to read , use multiple compilation units . ( A function cannot be placed in two compilation units , but more than two can be placed in a single unit , which is the CPP . cpp corresponds to one . obj, and then all obj is linked together (through a program called a linker ), an . exe, which is the program .

If a. cpp to use anotherWhat to do with functions defined by. cpp? Just in thisThe. cpp species write on his function statement.The rest of the work is done by the linker for you., you can call the function casually. The linker will allobj is connected., but what happens if you happen to have the same function or an external variable? How does he identify? generally not allowed in the same program, there are two identical function names or external variable names. But thankfully,, C + + can be qualified by a keyword called a link property, you have this function that is common to the entire program., or just in a compilation unitUsed in obj. These keywords areextern andStaticextern is the meaning of external linksExcept for this unit., the external unit is also able to access the function. Static is an internal link, self-owned unit. For so long,, not yet said. h's role?? actually no. h can also work very well, but when you find an external link to a function or an external variable, many statements are requiredBecauseC + + This language, when using functions and variables, he must be declared, why should I declareAfter the statement, he was known for his specs.In order to better discover the parts of the discord.. You're not delusional. A compilation unit, what information is automatically obtained from another compilation unitAnd know how you define this function.. So to speak, as long as the cells that use the function, you have to write a statement on that. cpp Inside, it's not a hassle.And, if you want to modify, you have to modify it one at a. It's so unbearable... H was born to solve the problem.That he contains these public things. Then all the functions that need to use the. cpp, you only need to use#include包含进去便可. Later needs to be modified, and just modify a copy of the content. Please be careful not to misuseDon't write code inside h,.h.,. h is notWarehouses for. cpp, everything is stuffed into the inside.. If you write code inside, when other. cpp when he was included., there is a case of duplicate definitions, such as the functionFunc () {printf}; put the header fileA.H, there are someA.cpp required declarations etc; and then you find outB.cpp need to useA function inside the A.cpp, it would be nice toA.h included in. Note, #include并不是什么申请指令 He is the content of the specified file a.cpp and b.cpp have a The Func () function is defined static then Fortunately , a waste of space; if it is extern, the external link (this is the default !; if you don't understand h, only with multiple cpp to see how to write CPP writes the same name function cpp use other

Header files are not compiled , generally just compiled . cpp generates . obj. However , there are #include将指定头文件in the CPP (in fact any file ) inserted into the complete form . CPP. If you do not like this way , you can also directly in the . cpp to write , and not put in . h , copy in. The header file is a tool , but not required .

Personal Opinion:
. CPP is used for (1) function implementations (including global functions (default), static functions).

(2) global variable, static variable definition.

(3) static function declaration, static variable declaration. (Specify functions and variables used inside the compilation unit)

Within. h for (1) Global function declarations, note that static functions are not included.

(2) global variable declaration, no static variable.

--------------------------------------------------------------------------------------------------------------

My Little Summary by Ordedere

As mentioned above,#include并不是什么申请指令, he is the content of the specified file , intact copy in. So we can connect the code of. h to the front end of the. CPP to see more clearly:

A.H, B.cpp & C.cpp both include A.H, then b.cpp can actually be seen as files: A.h+b.cpp connected to the CPP source code, and A.h+c.cpp connected to the source code equivalent to C.cpp include A.H.

So for the declaration of the variable in. h, the CPP has the following classification:

Declaring the variable int a in. h is equivalent to declaring (or defining) variables in the front end of the. CPP global variable int A;

Declare an external variable extern int A in. h, which is equivalent to declaring an external variable A at the front end of the. cpp, so this variable A is indicated in the other CPP file;

Declare global variables in. h static int A; Equivalent to the variable a inside the file in all CPP containing. h, and the address of the A variable in the CPP that contains the H file is different, that is, the space waste problem that the original author says.

Similarly, for a function declaration:

Declaring a static function in. h is equivalent to defining the static function in the CPP program that contains the header file;

Declare an external (extern) function in. h, which indicates that the function is defined in a different source file.

It is a good idea to declare a function in. h, that is, to declare it at the beginning of the CPP.

So: the declaration of a static function and a global static variable should be in CPP (after all, the static meaning is to ensure that the function or variable is scoped to the inside of the source file), and avoids the declaration of a static variable or function in. h.

The role of C + + *.h and *.cpp in compiling

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.