How is the header file connected to the corresponding .cpp file?

This question already has an answer here:

  • How does the compilation/linking process work? 5 answers
  • C++: Compiler and Linker functionality 8 answers

  • Short answer: there is no relationship between the header and its implementation. One can exist without the other, or the two could be placed in files with unrelated names.

    while compiling the main.cpp how will the compiler know to look for the definition of any function mentioned in yum.h in yum.cpp ?

    The compiler has no idea. Each time it sees a reference to something declared in yum.h , or in any other header file, for that matter, it stays on a lookout for the corresponding definition.

    If the definition is not there by the time the compiler has reached for the end of translation unit, it writes unsatisfied references into its main.o output, noting the places from which they are coming from. This is called a symbol table.

    Then the compiler compiles yum.cpp , finds definitions from yum.h in it, and writes their positions into yum.o 's symbol table.

    Once all cpp files have been processed, linker grabs all .o files, and builds a combined symbol table from them. If unsatisfied references remain, it issues an error. Otherwise, it links references from main.o with the corresponding symbols from yum.o , completing the process.

    Consider an example: let's say yum.h declares a global variable int yum = 0 defined in yum.cpp , and main.cpp prints that variable. The compiler produces main.o with a symbol table saying "I need int yum 's definition at address 1234", and yum.o file's symbol table saying "I have int yum at address 9876". Linker matches the "I need" with "I have" by placing 9876 at the address 1234.


    The compiler does not need to look for anything in other code files.
    The result of compilation is an object file, which has placeholders for whatever is defined in other code files. For making those placeholders, the compiler only needs the information provided by a suitable header (declarations, prototypes etc.).
    The step of filling in the placeholders is the job of the linker, which is theoretically separate tool from the compiler. ("theoretically" as in "could be a single tool behaving like two separate tools.)

    The reason for including the header with the declarations into the code file which does the implementing (of all or part of what gets declared) is a widely spread best practice, because it allows the compiler to complain if implementation and declaration do not match.


    So while compiling the main.cpp how will the compiler know to look for the definition of any function mentioned in yum.h in yum.cpp?

    Compiler does not. When it produces object file (this is result of compiling) it says there - call function such and such. Then there is linking process, where all object files and libraries are linked together to produce executable. As linker sees all object files and libs it know where to find that function (or produces error, if it cannot find it).

    ie how will compiler know to complie the yum.cpp file too as there is no reference to it in either the main.cpp file or yum.h file.

    Compiler does not know that either. It is programmer job to tell compiler to compile all necessary source file into object ones and pass them all to linker. It can be done inside IDE which could be hidden from you or you may have to do that manually using utility make for example.

    Also why should yum.h included in yum.cpp

    When you compile C++ program you usually have classes and their methods. To be able to use class in other source files it's definition must be visible there, so it goes to a header. Methods, when implemented inside .cpp file also must be declared inside class first, so that .cpp has to include it's header. And even for standalone functions it is just cleaner to let compiler match signature in header to signature in implementation.

    链接地址: http://www.djcxy.com/p/73304.html

    上一篇: 用一个简单的c ++程序编译过程

    下一篇: 头文件如何连接到相应的.cpp文件?