|
From: Richard A. <dic...@ho...> - 2001-04-09 10:32:55
|
>I'd be grateful if someone could write a few lines to >help me understand header files. there are basically two stages to the 'build' process - compiling and linking. In the first stage, the compiler looks at each source code (.cpp) file separately, and produces an object (.o) file from it. Because it deals with each file separately, it can't see the definitions of functions and classes in other source code files, so we have to help it along by #including header files which provide a brief rundown of the functions and classes that the compiler should *assume* exist in the other source code files (the header files give details of function argument lists, return types, functions provided by classes etc. so the compiler can check you've used the properly, but not what they actually do). In the second stage, the linker (usually a program called ld.exe) is given all of the object files produced by the compiler at once, along with any library (.a) files (such as the standard C or C++ libraries, various windows api libraries, opengl etc.) that it needs, and it glues them all together to produce the final .exe or .dll or whatever you're trying to create. >I know that #include is used to read source code from >a file into the current file, and that MyClass.h holds >the declaration of a class whereas MyClass.cpp holds >the definition of the class. The MyClass.cpp file has >a #include "MyClass.h" so that it reads in the class >declaration. >I'm a bit confused as to why, in any examples I see, a >main.cpp file uses #include "MyClass.h": shouldn't the >main.cpp file require both the class declaration and >its definition? (the compiler only needs to know the declaration of MyClass when compiling main.cpp - the linker will sort out connecting the function calls in main.cpp to the function definitions in MyClass.cpp) this obscure build mechanism can help to reduce build time - if you've only editted one .cpp file since the last build, you only need to compile that one file (it's object file will be the only one that's out of date) and then link the new object file with the old ones from the other source code files. in big projects this makes an enormous difference to build time. unfortunatly dev-c++ doesn't do this - it recompiles every .cpp file every time you build :O( anyway, hope this helps, richard _________________________________________________________________________ Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com. |