Role in C ++ header files and source files Detailed

A, C ++ compiler mode

In general, a C ++ program that contains only two types of files - .cpp file and .h files. Wherein, the file .cpp C ++ source file is referred to, which are put in C ++ source code; .h files were referred to as the C ++ header file, which is put in the C ++ source code.

C ++ Language Support "are compiled" (separatecompilation). That is, all of the contents of a program can be divided into different sections were placed in different .cpp file. .cpp file where things are relatively independent, does not need to communicate with other documents at the time of compilation (compile), only you need to be compiled into an object file with other object files and then do a link (link) on the line. For example, in the document it is defined in a global a.cpp function "void a () {}", and the file b.cpp need to call this function. Even so, files and documents b.cpp a.cpp do not need to know each other's existence, but they can be compiled separately, and then compiled into object files linked after the entire program can be run.

This is how to achieve it? Written from the perspective of the program is concerned, it is very simple. B.cpp in the file, the call to "void a ()" function before the first statement about the function "voida ();", on it. This is because the compiler at compile time b.cpp generates a symbol table (symbol table), like "void a ()" symbols not see such a definition, will be stored in this table. When another link, the compiler will be in another object file to find the definition of this symbol. Once found, the program will be successfully generated.

Note the mention of the two concepts, one is the "definition", is a "statement." Simply put, the "definition" is a symbol to describe the finished pieces: it is variable or function that returns what type, what parameters need and so on. The "statement" is just a statement of the existence of this symbol that tells the compiler that this symbol is defined in another file, first with me, when you link to try other places to find it in the end to see what is it. Defined time Yaoan C ++ syntax completely define a symbol (variable or function), and when it was declared only you need to write a prototype of this symbol. Note that, a symbol, the entire program can be declared many times, but will have to be defined and only once. Just think, if a symbol appears two different definitions, compiler who to listen to?

This mechanism for C ++ programmers who brings many benefits, but also leads to a method of writing programs. Consider, if there is a very handy function that "void f () {}", in many .cpp file of the entire program will be called, then we only need to define this function in a file, while in others the document declared that function on it. Fortunately, a function to deal with, it will declare a word. However, if the function much like a lot of mathematical functions, there are hundreds, then how do? To ensure that every programmer can totally put all the form functions are accurately written down and write out?

Second, what is the first document

Obviously, the answer is impossible. But there is a very simple way, programmers can help eliminating the need to remember so much trouble function prototype: we can declare statement that hundreds of functions are all first written, in a file, programmers need to wait until their time, put all these things into his copy of the source code.

This approach is certainly feasible, but still too much trouble, but also clumsy. Thus, the header will be able to play its role in the. The so-called header files, in fact, the contents of its content with the .cpp file is the same, are C ++ source code. But the header files without being compiled. We all put all the function declarations in a header file, when one .cpp source file need them, they can, through a macro command "#include" be included in this .cpp file, thereby merging their contents to .cpp file. When .cpp file is compiled, to be included into the role of these .h files will play a.

As an example it is assumed that all the mathematical functions are only two: f1 and f2, then we put them in the definition in math.cpp:

/* math.cpp */
double f1()
{
//do something here....
return;
}
double f2(double a)
{
//do something here...
return a * a;
}
/* end of math.cpp */

And the statement "these" functions are placed in a header file math.h:

/* math.h */
double f1();
double f2(double);
/* end of math.h */

In another file main.cpp, I want to call these two functions, then you only need to come in the header file that contains:

/* main.cpp */
#include "math.h"
main()
{
int number1 = f1();
int number2 = f2(number1);
}
/* end of main.cpp */

In this way, it is a complete procedure. Note that, .h files do not write after the compiler command, but it must be in place to find the compiler (such as with main.cpp in a directory) main.cpp and math.cpp are available through separately compile, main.o and math.o, and then link the two object files, the program can run.

三、#include

#include is a macro command from the C language, before it is compiled in the compiler that comes into play in the pre-compile time. #Include role is to document the contents of the written behind it, finished pieces, word for word included into the current file in the past. It is worth mentioning is that it itself is not any other role and function of vice, its role is to every place it appears, replace the contents of that file written behind it. Simple text replacement, and nothing else. Therefore, the first sentence (#include "math.h") main.cpp file, before compiling the contents will be replaced math.h file. That is when the compilation process will begin, the contents of main.cpp has changed:

/* ~main.cpp */
double f1();
double f2(double);
main()
{
int number1 = f1();
int number2 = f2(number1);
}
/* end of ~main.cpp */

Not too much, just right. The same can be seen, if we except main.cpp, there are many other .cpp file also used function f1 and f2, then they all just need to write a #include "math before using these two functions. h "on the line.

Fourth, the header file should write what

By the above discussion, we can understand the role of the header file is to be included into the other .cpp. They compiler itself is not involved, but in fact, their content has been compiled in multiple .cpp files. By "is defined only once" rule, we can easily draw, put the header files should only declare variables and functions, but can not put their definitions. Because the contents of a header file will actually be introduced into a number of different .cpp file, and they will be compiled. Of course the statement put right, if you put the definition, then it is equivalent to appear in more than one file in the definition of a symbol (variable or function), and even if these definitions are the same, but for the compiler to do so illegal.

Therefore, we should keep in mind that, .h header file, declare a variable or function can only exist, but do not put definitions. That is, the form can only be written in the header file: extern int a; and void f (); sentences. These are the statements. If the write INTA; or void f () {} such a sentence, then once this is the first of two or more file .cpp file included, the compiler error immediately. (About extern, we have discussed earlier, there is no longer discuss the differences with the definition of the statement.)

However, this rule is there are three exceptions:

  • First, the definition of header files can be written const object. Because the global default is no extern const object declared, so it is only valid in the current file. Such objects are written into the header file, even if it is to contain a number of other .cpp file, this object is also only in that it contains a valid document, other files are not visible, so they will not It will lead to multiple definitions. At the same time, because these .cpp files are included into the object from a header file, which will ensure that the value of these .cpp file of the const object is the same sense. Similarly, the definition of static objects can be placed in the header file.

  • Second, the file header may be written inline function definition (inline) a. Because the inline function is required where the compiler encounters it in accordance with its definition it inline expansion, rather than as an ordinary function can be declared re-link (inline function is not linked), so the compiler needs to see the full definition of an inline function job at compile time. If an inline function like a normal function can only be defined once, then this thing easy ones. Fortunately, because in a file, I can define an inline function written in the beginning, so you can ensure use of the time can be seen behind the definition; however, if I have to use this function in other documents that how to do it? This is almost no good solution, and therefore the provisions of C ++, inline functions can be defined in the program as many times as inline functions in a .cpp file appears only once, and in all .cpp file, this inline defined function is the same, it will be able to compile. So obviously, the definition of an inline function into a header file is very wise.

  • Third, the definition of header files can be written class (class) is. Because when you create an object of a class in a program, the compiler only if the definition of the class of fully visible, in order to know how to objects of this class should layout, so the requirements for the class definition, with the inline function is fundamental the same. So the class definition into a header file, use the .cpp file of this class to include this header file, it is a good practice. Here, it is worth mentioning that a class definition contains the data members and function members. Data members is to wait until a specific object will be defined (allocated space) when it is created, but it is a function of the members need to be defined at the outset, and this is achieved we usually refer to the class. Generally, our approach is that the class definition in header files, and the function member implementation code in a .cpp file. It is possible, and it is a good solution. However, there is another way. That is a direct function of the members of the implementation code is also written inside the class definition. In the C ++ class, if the function is defined in the definition of the body member of the class, the compiler will view the inline function. Therefore, the definition of the class definition written into the body of the function members, put together a header file, is legal. Note that, if the members of the defined functions written in the class definition in the header file, but not written into the class definition, which is illegal, because this function at this time is not a member of the inline. Once the header is two or .cpp file contains this function member was redefined.

Fifth, the header file protection measures

Consider, if the header file containing only the declaration, then it is a .cpp file that contains the same again many times no problem - because of the emergence declaration statement is unrestricted. However, the header file discussed above with three exceptions in the header file is a very common use. Then, once a header file appears in any of the above three exceptions, it was again a .cpp contain multiple words, the problem is big. Because the syntax elements in these three exceptions though "can be defined in multiple source files", but "only appear once in a source file." Imagine, if the definition of class A contained ah, bh containing defined in class B, since the definition of A class B dependent, so the #include also ah bh. Now there is a source, it also uses the class A and Classes B, then the programmer to both the source file contains came ah, bh also contains the came. At this point, the question came: the definition of class A appears twice in the source file! So the whole program will not compile a. You might think this is a programmer's mistake - he should know bh contains ah - but in fact he should not know.

Use "#define" with the conditional compilation can solve this problem. In a header file, defined by a #define name, and through conditional compilation #ifndef ... # endif so that the compiler can be defined in terms of whether or not the name, and then decide whether or not to continue in subsequent compile the header file contents. This method is simple, but we must remember to write into the write header files.

The difference between C ++ header files and source files

First, how to associate the source file headers #include according to

1, the system comes with a file header in angle brackets, the compiler will look in the file system directory.

2, the file user-defined enclosed, the compiler will first look double quotation marks in the user directory, and then to C ++ installation directory (such as VC can specify and modify the library search path, Unix and Linux through environment variables set), and last look in the file system.
#include "xxx.h" (I always thought, "" and <> no difference, but tinyxml.h all files in a non-system, so use the "")

Second, how to associate the source file header

The question really is, known to the header file "ah" declares a set of functions, "b.cpp" to achieve these functions, so if I want to use the "ah" declared in "c.cpp" These function implemented in the "b.cpp" usually are using #include "ah" in "c.cpp", then c.cpp how to achieve b.cpp find in it?

In fact, .cpp and .h file name does not have any direct relationship, many compilers can accept other extensions. For example, even now see the crash and the company's source code, .cpp file replaced by the .cc file.

In Turbo C, the use of the command line to compile the command line argument for the name of the file, the default is .cpp and .h, but can also be customized to .xxx and so on.

Hao strong teacher of "C Programming" a book that, when the pre-compiler to #include command of "file contains the processing": Copy the entire contents of file2.c to #include "file2.c" place. This also explains why many compilers do not care in the end what the file extension is ---- because #include preprocessing is to complete a "copy and insert the code" work.

Compile time, and will not go to realize the function b.cpp file, this work is carried out only at link time. We b.cpp or c.cpp with #include "ah" is actually related to the introduction of the statement so that the compiler can, does not care where the implementation is, it is how to achieve. After the source file is compiled into object files green (.o or .obj file) the target file, these functions and variables treated as a symbol. When the link, which need to be described or .obj .o file (here b.cpp generated .obj or .o file) must be connected to the inside makefile, this time, to the connector will .o file or .obj find the function implemented in b.cpp in, and then build them into the makefile that can be specified executable file.

Under Unix, even not in the source file includes header files, only named in the makefile can (but this greatly reduces the readability of the program, is a bad habit oh ^ _ ^). In VC, a gang without the need to write your own makefile, only need to file required are included in the project, the VC will automatically help you write the makefile.

Usually, C ++ compiler in each .o or .obj files to find what symbols are needed, rather than looking to find or not find only in a certain file. Thus, if implemented in several different files in the same function, the same or define a global variable, when the link will prompt "redefined".

above all

.h file can contain:

  • Declare class data members, but can not be assigned

  • Static data members of the class definition and assignment, but not recommended, just like the statement.

  • Declare member functions of the class

  • Declaration of non-class member function

  • Defining constants: such as: constint a = 5;

  • Define a static function

  • Inline function definition class

It can not contain:

1. All non-static variables (data not members of the class) declaration

2. Do not default namespace declaration in header files, using namespace std; etc. should be placed in the .cpp, .h file in use std :: string

 

You may also be interested in the article:

Article simultaneous release:  https://www.geek-share.com/detail/2769307191.html

Guess you like

Origin www.cnblogs.com/xxcn/p/10930105.html