Why do we include header files and not source files?

Colby Veenman

I've seen similar questions asked yet they still do not make sense to my ape brain.

Here is an example. If I declared a function in a header file named Bob.h: void PrintSomething(); and in the .cpp file I say: void MyClass::PrintSomething(){std::cout << "Hello";} . I've seen people in another .cpp file for example Frank.cpp, only include the Bob.h header which just has the declaration (No code inside it) and not the .cpp with the code but then what blows my mind is when they call the PrintSomething() function in Frank.cpp it uses the code from Bob.cpp and prints "Hello". How? How does it print "Hello" which was added in the .cpp file when I've only included the .h file which doesn't say anything about "Hello", its just a declaration? I've looked through the compile process and linking process too but it just doesn't stick.

On top of which if I were to now say in my Frank.cpp file: void MyClass::PrintSomething(){std::cout << "Bye";} and included the Bob.h file in my main.cpp and called the PrintSomething() function would it print "Hello" or "Bye"? Is the computer psychic or something? This concept is the one thing I am not grasping in my C++ learning journey.

Thanks in advance.

Hatted Rooster

The moment you include Bob.h the compiler has everything it needs to know about PrintSomething(), it only need a declaration of the function. Frank.cpp does not need to know about Bob.cpp which defines PrintSomething().

All of your individual cpp files output object files generated by the compiler. These in themselves don't do much until they're all glued together, this is the linker's responsibility.

The linker takes all your object files and fills in the missing parts:

Linker talk:

Hey, I see that Frank.obj uses PrintSomething() and I can't see its definition in that object file.

Let's check the other object files..

Upon inspecting Bob.obj I can see that this contains a usable definition for PrintSomething(), let's use that.

This is of course simplified but that's what a linker does in short.

After this is done you get your usable executable.


on top of which if I were to now say in my Frank.cpp file: void MyClass::PrintSomething(){std::cout << "Bye";} and included the Bob.h file in my main.cpp and called the PrintSomething() function would it print "Hello" or "Bye"? Is the computer psychic or something?

The linker would find 2 definitions of PrintSomething() and would emit an error, it has no way to know what definition is the right one to pick.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C: Why do we include header files, which declare but don't define?

From Dev

why include standard header files?

From Dev

Why do we include TTF files in font-face declaration?

From Dev

Why are there multiple copies of header files in /usr/include?

From Dev

Why do we need *.lib files?

From Dev

Why do we need *.lib files?

From Dev

Why do we have .desktop files?

From Dev

header and source files in jni

From Dev

C Header include-guards used only in header files but not in .c files. Why?

From Dev

ways to include many header files

From Dev

Is it ok to include files into a header file

From Dev

Why Wix MSI doesn't include the source files and looks for source files somewhere else?

From Dev

Why use .gitignore? And why do I need to not include certain files?

From Dev

C header and source files structure

From Dev

Why are . and .. are output as a hidden files when we do ls -a?

From Dev

Why do we need two different conf files in spark?

From Dev

How do I include header files from /usr/include in my cmake project?

From Dev

C++:: #include:ing template class header file in multiple source files?

From Dev

How to include struct variable declared in a header file in more than one source files?

From Dev

How to include struct variable declared in a header file in more than one source files?

From Dev

Why do I need to include .o files when compiling?

From Dev

Why do linux kernels include files for all devices?

From Dev

Why do I need to include .o files when compiling?

From Java

Why have header files and .cpp files?

From Dev

How to search for header files in usr/include/linux

From Java

How to determine which header files to include?

From Dev

GCC not finding header files in /usr/local/include

From Dev

#include C++ header files in opencv

From Dev

Can't include NDK header files

Related Related

  1. 1

    C: Why do we include header files, which declare but don't define?

  2. 2

    why include standard header files?

  3. 3

    Why do we include TTF files in font-face declaration?

  4. 4

    Why are there multiple copies of header files in /usr/include?

  5. 5

    Why do we need *.lib files?

  6. 6

    Why do we need *.lib files?

  7. 7

    Why do we have .desktop files?

  8. 8

    header and source files in jni

  9. 9

    C Header include-guards used only in header files but not in .c files. Why?

  10. 10

    ways to include many header files

  11. 11

    Is it ok to include files into a header file

  12. 12

    Why Wix MSI doesn't include the source files and looks for source files somewhere else?

  13. 13

    Why use .gitignore? And why do I need to not include certain files?

  14. 14

    C header and source files structure

  15. 15

    Why are . and .. are output as a hidden files when we do ls -a?

  16. 16

    Why do we need two different conf files in spark?

  17. 17

    How do I include header files from /usr/include in my cmake project?

  18. 18

    C++:: #include:ing template class header file in multiple source files?

  19. 19

    How to include struct variable declared in a header file in more than one source files?

  20. 20

    How to include struct variable declared in a header file in more than one source files?

  21. 21

    Why do I need to include .o files when compiling?

  22. 22

    Why do linux kernels include files for all devices?

  23. 23

    Why do I need to include .o files when compiling?

  24. 24

    Why have header files and .cpp files?

  25. 25

    How to search for header files in usr/include/linux

  26. 26

    How to determine which header files to include?

  27. 27

    GCC not finding header files in /usr/local/include

  28. 28

    #include C++ header files in opencv

  29. 29

    Can't include NDK header files

HotTag

Archive