I had initially inquired about why .h
files are necessary for C++. I had a question regarding why not just do #include /file.cpp
instead (where file.cpp
would contain function/class declarations and definitions), but then I found this link which stated that doing this would surmount to essentially copying all code from the file.cpp
. So if I'm understanding this right:
- The confusion/error comes from linking. So if I just compiled the main file
#include /file.cpp
, but not the actualfile.cpp
itself, wouldn't I prevent this error from occuring? Or is it such that you automatically compile AND link anything that you#include
? - If it is possible, then is the reason why we don't do this because in large projects, it is easier to compile a
.h
file that has been listed to#include
instead of a.cpp
file, even though the contents of the.cpp
file will be called many times (ie. the.cpp
file only has to be compiled once)?