hi, what exactly is the function of a header file? i wrote a program with two source codes, and linked them with a #include "header.h" comment. i compiled these, then went to g++ and said
g++ test1.o test2.o -o output.run
and it worked fine. just for curiosity, i deleted the header file "header.h" and tried the above command again. to my surprise, it worked just fine, once again.
so when exactly does the header file have its function? what is it really for? thanks
it would work the second time because you didn't try to recompile first. the second time all you did was to ask g++ to link the two object files to produce the ouput file.
"The header file allows you to separate declaration from implementation of your classes/functions. This is used to reduce code because a header file is actually loaded into the location of the #include during compilation. It also prevents (mostly) cyclic redundancy where the same header file is included more than once through a series of #includes. Without the header file you'd never be able to have 2 files that include each other."