I am a c++ newbie. I have prior experience with python, but c++ is my second language. Right now, I'm learning about how to use header and source files to aid organization and compiling time for projects. I understand what the correct syntax is for referencing header files (#include "foo.h"), but I don't understand how the header file itself refers to the source file (foo.cpp) in main.cpp. I don't see any reference in the header file to foo.cpp, and also in main.cpp. How does the compiler know to stitch foo.h together with foo.cpp using #include? Apologies if this question is redundant. I tried looking this up already. I found a lot of information on how to use header and source files, but no good explanation of how they actually work.
-
The compiler does what you tell it. It does not stitch on its own. – drescherjm Jun 02 '17 at 19:35
-
2Header files do not reference source files at all. Source files reference header files with `#include`. – cdhowie Jun 02 '17 at 19:36
-
Header files do not reference source files and the compiler doesn't know what source files to compile together. You have to tell the compiler what source files to use to build your project. – NathanOliver Jun 02 '17 at 19:36
-
Also worth a read: https://stackoverflow.com/questions/5904530/how-do-header-and-source-files-in-c-work. C and C++ are different languages, but in this case they cross over well enough for the C answer to apply. – user4581301 Jun 02 '17 at 19:42
2 Answers
It doesn't.
First of all, just because there's a foo.h, doesn't mean that there's a foo.cpp.
So, your real question is "How does the compiler know to put main.cpp and foo.cpp into the same executable?" Basically, because you tell them to.
This is quite different on different OSes and compilers and build systems, but basically, you do one of two things.
a) You compile the file separately, and then link them together:
cl main.cpp
cl foo.cpp
link main.obj foo.obj
b) Compile them together:
cl main.cpp foo.cpp
(This is really just a shortcut to the above, with the compiler doing the individual steps automatically)

- 101,701
- 37
- 181
- 258
They don't.
Each .c
or .cpp
file is a single compilation unit that produces an obj file (.o
). This is all the compiler does. Each obj file may contain unresolved references. For example, if your main.cpp
uses a function foo()
that's declared in foo.h
and implemented in foo.cpp
, then foo.o
will contain the compiled code for foo()
, and main.o
will contain the compiled code for main()
which includes a call to something called foo()
. However, main.o
does not know where foo()
actually is, and it doesn't know about foo.o
.
The obj files are then linked together by the linker. This is when the unresolved references in the obj are resolved. The linker takes main.o
and foo.o
as its input, and the linker then produces whatever.exe
with the pieces connected. If a function is unresolvable (e.g. you forgot to include foo.o
in the linker call) or a function exists with the same name in multiple obj files, the linker will produce an error.

- 12,528
- 1
- 24
- 58