The relationship between the header file and the implementation file in C + +

Source: Internet
Author: User

C++The relationship between the middle header file and the implementation file about the relationship between the two before, from N years ago~Long Longago,once aupon a time .... It was a forgotten age, in which the compiler knew only. C (. cpp)) files without knowing. h is the age of what. At that time, people wrote a lot of. C (. cpp) files, and gradually, people found that the declarations in many. C (. cpp) files are the same, but they have to put them into each. C (. cpp) file in a single word and repeatedly. But even more frightening is that when one of the declarations changes, you need to check all the. C (. cpp) files and modify the declarations in them .~ It's the end of the world!Finally, someone (perhaps someone) can no longer endure such torture, he or she extracts the repeating part, puts it in a new file, and then, in the desired. C (. cpp) file, typing a statement such as # include XXXX. This way, even if a declaration has changed, there is no need to look for and modify---the world is still so beautiful! Because this new file is often placed on the head of A. C (. cpp) file, it is called a "header file" and the extension is. h. From then on, the compiler (in fact, the preprocessor) knows that the world has a. C (. cpp) file, as well as a. h file, and a named # include command. Although a lot of changes have occurred later, but this usage has continued to this day, only a long time, people will forget the reason for that year. When it comes to the head file, let's talk about its role.~think of the high quality C written by Lin Rui Gg/c++A brief description of the function of the top file: (1) to invoke the library function through the header file. In many cases, the source code is inconvenient (or not) to the user, as long as the user is provided with a header file and a binary library. The user only needs to invoke the library function according to the interface declaration in the header file, without having to worry about how the interface is implemented. The compiler extracts the appropriate code from the library. (2) header files can enforce type safety checks. If an interface is implemented or used in a way that is inconsistent with the declaration in the header file, the compiler will point out the error, a simple rule that can greatly reduce the burden of debugging and error-changing by the programmer. Preprocessing is the predecessor of the compiler, the function is to store in different files of the program module set into a complete source program. #include本身只是一个简单的文件包含 preprocessing command, that is, to put the following file of the include in this command here, there is no other use ( At least I think so. I have a smile on the universe of the view, very agree, the foundation of the east must be clear. I'm going to tell you the story of a laughing brother, complete his some confusing time~Example://a.hvoidfoo ();//A.C#include"a.h"  //My question comes out: Is this sentence to be, or not? voidfoo () {return;}//main.c#include"a.h"intMainintargcChar*argv[])   {foo (); return 0;} For the above code, please answer three questions: #include in A.C"a.h"is this sentence superfluous? 1Why do you often see the xx.c included in the corresponding xx.h? 2if A.C is not written, will the compiler automatically bind the contents of the. h file with the. c file of the same name? 3The third question I changed for him: if A.C does not write include<>, does the compiler automatically bind the contents of the. h file with the. c file of the same name? Here are the words of a smile: from the C compiler's point of view,. h and. C are clouds, that is, the name of. txt,. doc is not a big difference. In other words, the. h and. C Have nothing to do with it. The declarations of variables, arrays, and functions defined in the. c file with the same name are generally placed in H. Declarations that need to be used externally by. C. What's the use of this statement? Just make it easy to quote where these statements are needed. Because #include"xx.h"The actual meaning of this macro is to delete the current line and insert the contents of the xx.h into the current line position intact. Because there are a lot of places to write these function declarations (every call to a function in XX.C is declared immediately before use), so use #include"xx.h"This macro simplifies a lot of line code--and lets the preprocessor replace itself.    In other words, xx.h actually just makes the call where the function declaration in the XX.C needs to be written (fewer lines can be written), as for the include. h file who, is it. h or. c, or the. h with the same name. C, there is no inevitable relationship. So you might say, "Ah?" I usually just want to call a function in xx.c, but include the Xx.h file, is not a macro replacement after a lot of useless declarations? Yes, it does introduce a lot of rubbish, but it saves you a lot of ink, and the whole layout looks more refreshing. It is this truth that the fish and bear cake cannot be combined. Anyway, more statements (. h is generally only used to put the declaration, but not defined, see my book fixing "cross the road, look Around") also harmless, do not affect the compilation, why not? Turn back and look at the top 3 questions, very good answer it? Here's the answer: answer:1. Not necessarily. This example is obviously superfluous. But if the functions in. c also need to call the same. C, then this. C will often include. h with the same name, so that there is no need to worry about the order of declarations and calls (the C language requires that it must be declared before it is used, and include has the same name.) h is usually placed at the beginning of. C. There are many projects that even use this notation as code specifications to standardize clear code. 2Answer: 1 has already answered. 3. Answer: No. The person who asks this question is definitely unclear, or wants to mix water to touch fish. Very annoying is a lot of Chinese test out of this bad problem, for fear that others have a clear concept, absolutely to the examinee dizzy. over!To be clear here, the compiler compiles according to the compilation unit, the so-called compilation unit, refers to a. c file and all the. h files it contains. The most intuitive understanding is that a file, a project can contain many files, which have a program entry point, That is what we usually call the main () function (of course, you can not have this function, the program can still start, see my blog in detail). In the absence of this program entry point, the compilation unit only generates the object file (. o file, under Windows called. obj). This example contains a total of two compilation units, A.C,MAIN.C, which, as I say, only generate their own. o files during the compilation phase. This phase does not have any relationship with other files. and include this preprocessing instruction occurs in the preprocessing phase (the previous compilation phase, Just a precursor handler for the compiler): H. c is not necessarily a cloud, out of the compiler to talk about these do not have any meaning, put aside the deeper of these, for example, the OS how to start this file, the PE structure (Linux elf) and so the compiler first to identify this file can be compiled it, This is the premise. If you change its extension, will your compiler know about it?~ Rise to a higher level to look at this problem, xx brother Said is also good ~ I think xx brother said the meaning is the two can not because the name of the same to think of what the relationship between the name is casual ~the connection between the two, I said earlier, is due to historical reasons, coupled with the habits of people, I think no one would like to remember so many filenames it. (Take me for example, if a data table more than 30 fields, I think the head is big, now the table has up to hundreds of fields, I really hope that high man to study what a good way to~, also make our world a little better ~The third question of the universe is very representative, many times on the internet to see, now the compiler is absolutely not so smart, and there is no need to do so. Let's talk about the compiler's process. (I think beginners have a problem with this, that is, for the compilation process. H. C (. cpp) changes are not well understood,) below I said to give a simple example to talk about~examples are as follows://a.hclassa{pubic:intFintt);};//A.cpp#include"a.h"intA::f (intt) {    returnt;}//main.cpp#include"a.h"voidMain () {a A; A.F (3);} During the preprocessing phase, the preprocessor sees # include"file name"Just read this file in, like it compiles main.cpp, see # include"a.h", it reads the contents of the A.H, and it knows that there is a class A that contains a member function F, which takes an int parameter and returns a value of type int. It is easy to compile a line of a to understand, it knows is to take a this class on the stack to generate an object. Further down, it knows the following to call A's member function f, the parameter is 3, because it knows that the function to a number of parameters, this 3 exactly match, then just put it on the stack, generate a call F (int) function (usually a call), as for this f (intThe function is exactly where, it does not know, it is left empty, the link is resolved again. It also knows that f (intfunction to return an int, so maybe it's also ready for this (in the example, we don't use this return value, and maybe it doesn't). Then down to the end of the file main.cpp compiled, generated main.obj. There is no need to know the content of A.cpp throughout the compilation process. Similarly, the compiler compiles a.cpp, compiles the F () function, compiles the a.cpp, and compiles the F (). Generated a a.obj. The final step is the link stage, where the linker links all. obj generated by all. CPP in the project. In this step, it is clear that f (int) The address where the function is implemented, fill in the correct address with the address where the main.obj is empty. The executable file Main.exe is eventually generated. You got it?Do not understand that will say a few more words, we learn to compile the principle of the time all know that the compiler is staged, each phase of the source program from one representation to another representation, in general, the following order: source program, lexical splitter---parser, Semantic Analyzer- > code generator, Code optimizer, intermediate code generator,The target program. The two main activities that are involved in this intermediate 6 activities are the symbol Manager and the error handler. In the root cause, here is a symbol table of the east in the inside to let you possessed like not understand, In fact, the symbol table is a data structure. The basic function of a compiler is to record the identifiers used in the source program and collect various property information related to each identifier. The property information indicates where the identifier is stored/type/scope (valid at that stage) and so on, in layman's words, when the compiler sees a symbolic declaration, such as your function name, it puts it in the symbol table and registers it with the entry-and-exit address of your function, the number of arguments, the return information, and a bunch of things ~in the connection phase, the symbolic table in the project and the corresponding processing relation of the call are dealt with, that is, we usually refer to the solution reference. Through the front, do not know whether or not?finally quoted the end of the brother xxx three points: make clear grammar and concept easy also easy, say difficult also difficult. The trick has three points:1do not faint head work, take the time to think more, read more books;2read books, ask people to ask the strongman. Bad books and rotten people will give you a wrong concept, misleading you;3. Qinnengbuzhuo is a good training, a point of hard work; http://xiangyanglai.blog.163.com/blog/static/2047252022012715103338279/

The relationship between the header file and the implementation file in C + +

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.