0

How are header files connected to the cpp files? I have a cpp file, including header files. I understand what include does, but what about the cpp file of the header file?

Let's say:

calculate.cpp:

#include table.h

What happens with table .cpp? To fully understand calculate.cpp, is table.cpp also needed to be looked at?

sentence
  • 8,213
  • 4
  • 31
  • 40
Beefzone
  • 3
  • 4
  • 1
    *understand what include does* - it does not look like you do... Preprocessor builds text of the translation unit by substituting preprocessor directives, such as `#include`, starting from one file that works as a starting point. So those files that are used as starting points typically have `.cpp` extension while those files that are included in other files typically have `.h` / `.hpp` extensions and are called header files. C++ language does not specify how program content is supposed to be distributed between those files. – user7860670 May 06 '19 at 08:38
  • `#include` literally only means "include the contents of the file here, and proceed as if it were part of this file". There is no connection between files or their contents whatsoever, and the naming is only a convention. – molbdnilo May 06 '19 at 09:38

1 Answers1

1

You have file A.cpp which includes B.h. When you compile A.cpp the preprocessor will include everything from file B.h to the translation unit of A.cpp and the compiler create an object file from it.

The compiler doesn't care at this point about the implementation of whatever is in B.cpp. This is dealt with separately when the compiler compiles the translation unit B.cpp. The compiler trusts at this point that in the future (at link time) there will be something when calling something from B. If not, you will end up with a linker error (undefined symbols most likely).

Here you have a very good answer on what's happening: How does the compilation/linking process work?

But just to describe it in less words:

  1. Preprocessor: reads through your .cpp and included .h files (e.g. A.cpp and B.h and creates an output which the compiler then can compile. This will independently also happen for B.cpp and its includes/defines)
  2. Compiler: Takes the output from the preprocessor and creates object files. Object files contain mostly machine code and some linker information
  3. Linker: Links the object files together so when you run the program they right functions are called.

So I guess the connection you are looking for happens in the Linking stage. That's where all the pieces come together.

Philipp
  • 2,376
  • 5
  • 29
  • 47
  • So, in the end, there will be a connection. If I look after a program A.cpp, then I also have to check B.h AND B.cpp in order to understand what it does - did I get this right? – Beefzone May 06 '19 at 08:50
  • What do you mean by 'I also have to check B.h AND B.cpp'? – Philipp May 06 '19 at 08:51
  • Well, I currently have the task to work through some existing code. There is the A.cpp that I have to check, what it does. It includes B.h and there is also a B.cpp given. B.cpp includes C.h and C.cpp is also given. -> What files do I have to look through ? – Beefzone May 06 '19 at 08:55
  • If your `A.cpp` code calls functions that are implemented in `B.cpp` and you want to understand exactly what is happening in `A.cpp` then of course by extension you would also want to know what happens in `B.cpp`, or `C.cpp`. – Philipp May 06 '19 at 08:57
  • But where I get stuck is, A.cpp calls B.h. Where is now the connection between B.h and B.cpp, is B.cpp automatically also included ? – Beefzone May 06 '19 at 12:27
  • I tried to expand the answer a little bit - I really recommend to read the linked answer there. – Philipp May 06 '19 at 13:18