1.c++ source code is how to become a program.
(1) After we have written the source code, we first need to give the source code to the compiler, the compiler first preprocessing, that is, the processing of macros, the introduction of the # include directive introduced all the introduction of the header, generating a compilation unit. The compilation unit is the compiler's real work object and is a true C + + object.
(2) The general compilation mode will be compiled separately, then we must ensure that all declarations have consistency, the connector program helps us to all the compiled parts are bound together, so that all objects, functions are not ambiguous, these are the program before the end of the run. Of course, there is also the ability to add new code after the program is run (dynamic connection).
2. Two or three things when connecting (you must ensure that all declarations refer to the same entity)
(1) When a variable is preceded by extern, it is declared, but no extern is defined. A declaration can be multiple times, but must be consistent, and the definition can exist only once.
//file1.cintx =1;intb =1;extern intC;//file2.cintX//error: equivalent to int x = 0, so x is defined two timesextern Doubleb//Error: The declared type is inconsistent with the type defined in File1extern intC//error: Only declaration, not defined
(2) An inline function needs to be defined in each compilation unit that uses it, and the definition must be the same. Therefore, the inline function cannot use the extern declaration.
// file1.c inline int F (i) {return I;} extern inline int g (i); int H (int i) { return g (i);} // error: g (i) not defined in this compilation unit // file2.c inline int f (i) {return i+1 ;} // error: File2 differs from file1 with F (i) defined
(3) const and typedef are internal connections, that is, the scope is only used in the compilation unit where the name is located, generally in order to ensure consistency, the global const and inline functions in the header file. extern allows the const to obtain external link properties.
// file1.c extern Const int the ; // file2.c extern Const int A; void f () { cout<<a<<Endl;}
(4) You will also get the internal connection attribute in namespace without a name.
3. How to use the header file
(1) "#include <T>" represents the included standard library header file, "include" T.h "" means that the included header file is from a local file.
(2) There must be no regular function definitions, regular variable definitions, array definitions, namespace with no names, and exported template definitions in the header file.
(3) Single definition rule (ODR): A class, template, etc. in a standard can only have a unique definition.
//file1.cstructs{//correct: 1. Two struct s defined in a different compilation unit intA//correct: 2. They correspond to the same as the word one by one intb//correct: 3. The semantics of a word are also consistent in two files}voidF (s*);//file2.cstructs{//so these two struct S definitions are accepted as the same unique intA//Semantic Instances intb;}voidF (s* p) {/*...*/};
(4) When there is only one declaration, you can use the export template.
// file1.cExport template<class t> t twice (T T) {return t+T;} // file2.ctemplate<class t> t twice (T T); int g (int i) {return twice (i);}
(4) To avoid multiple occurrences of the same header file, we use a header file protector.
//
When Error.h is first seen at compile time, its contents are read into, giving Calc_error a value
When it is compiled again, its contents are ignored
#ifndef Calc_error_h #define Calc_error_hnamespace error{ //... }#endif
However, we will try to minimize the inclusion dependency, and the excessive use of the header file protector introduces a large number of declarations and macro definitions and causes the compilation time to be too long.
4. How to connect non-C + + code (c for example)
We generally use extern to give a connection convention to non-C + + code. For example, to connect the C language code, we use extern "C" as a connection convention and do not affect the function semantics, but we still have to follow the C + + type checking and parameter conversion. We also use a connection block containing C header files and code so that the entire file can be used by C + +.
extern " C " { <string.h> char *strcpr (charconstchar *); int G1; extern int G2;}
Here's how to use a function pointer and connect:
extern "C"{typedefint(*CFT) (Const void*,Const void*); voidQsortvoid*p, size_t N, size_t sz, CFT CMP);//CMP has a C connection}intCompare (Const void*,Const void*);//has a C + + connectionextern "C" intC_compare (Const void*,Const void*);//with C connectionvoidFChar* V,intSZ) {qsort (V, SZ,1, compare);//ErrorQsort (V, SZ,1, C_compare);//correct}
5. Global variable or less
For global variables in different compilation units, the order of initialization is not guaranteed, minimizing the use of global variables. A reference returned by a function can be used as an alternative to a global variable:
int& count () { staticint0; return UC;} void f () { for (int0; i <; i++) { cout <<++count () <<Endl; }}
Coding Road--Re-learning C + + (3): A re-understanding of compilation and linking