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.
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)
Comments
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.
#include.