|
Including Header Files and their cpp counterparts
Something I keep seeing, in tutorials, and even on here, is this.
When you make a seperate file for code, such as a class to use in a file, most examples go like this:
someclass.h - This is where you define the class and prototype the functions
someclass.cpp - This is where you define the functions of the class
main.cpp - where the main program runs from.
Inside the someclass.h you do the prototyping and class defining, no includes are needed beyond normal ones, such as <iostream> etc..
Inside the someclass.cpp you include the someclass.h then do the definitions of the class functions etc.
Inside the main.cpp file you include the someclass.h but do NOT include the someclass.cpp. (this is the part that is confusing me.)
If i understand correctly, the compiler compiles the code in the file, and when it finds an include, it jumps to that file, then returns to the main file. Most tutorials include the header file, but not the cpp file. This suggests to me that while the class get's defined, and it's functions prototyped, the cpp file with the function definitions never gets included.
So how exactly does the compiler work? does it automatically look for a header's cpp counterpart? Or do you need to compile those into obj's seperately and link them into the executable?
I'm using the borland free command line tools, wich is handy, but unless i learn makefiles, i don't see how to compile those extra cpp files into objects and link them in..
Anyone able to clear this up for me?
---------------------
edit::
I just tested it, and found that it would give me an error just including the header file. I changed it to include only the cpp file (which in turn included the header file) and it worked fine.
While this clears up how the compiler works, and it is what i expected, this still confuses me in that almost all tutorials say to include the header, but not the cpp counterpart. Perhaps these are to be compiled seperately and linked in? If so, how would that work?
Again, i'm using the borland free command line tools with the Free Crimson Editor IDE.. is there a way to setup the crimson editor to do this? perhaps someone could help me figgure out how make files work? Maybe the make files are what I need. Or is there some other way?
Last edited by Rafkin; 01-31-2005 at 11:48 PM.
|