Including Files

Started by
1 comment, last by intransigent-seal 15 years, 10 months ago
alright, I couldn't think of a good thread name so bear with me if you were mislead. While going through a C++ book I noticed that it split up its Classes by putting the declaration in a header file, and the actual methods in C++ files which include the headers. But im stumped, in the main file I only include the headers, so how do the C++ files get included? The code works, but i dont know why. ie. /* Class header */ class BaseClass { public: int Test(void); }; /* Class C++ file */ #include "Class.h" int BaseClass::Test(void) { return 5; } /* Main File */ #include "Class.h" BaseClass base; base.Test();
-A.K.A SteveGravity is unreliable-DTD
Advertisement
A 'source file' is what is resulted after running the preprocessor on each individual .cpp file (assuming you use .cpp files). Every source file is compiled separately as if the others did not exist. After all source files are compiled they are linked together. An IDE typically does all of this automatically assuming all the files are included in the project. So if your project viewer shows two .cpp files then the IDE does all the work. Hope that helps a little.

edit: More information? Oh why not.

When each source file is compiled every identifier that appears must be declared before use. This is what header files accomplish. They automate the process of declaring things. If you want to declare the same things in two or more source files you should use headers.

Whenever you call a function the compiler asks "is this function declared?" and if not then the compiler issues an error. This is called "undeclared". Later on, during the linker stage, the linker asks "is this function defined?" and then it examines all compiled source files for the defined function. If the linker finds more than one function defined it issues an error. This is called 'multiple definition". If the linker does not find the defined function it issues an error. This is called "undefined". The linker will additionally examine all library files you told it to look through. Libraries are treated like compiled source files in this regard.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Each .cpp file is compiled separately. In order to compile correctly, they must have all the relevant information about what functions are available, how each type is layed out in memory and a whole load of other stuff. Typically, that information is shared by placing it in header files which are then included into each .cpp file that needs that information.

Compiling a .cpp file produces an object file (.o if you're using gcc, .obj if you're using MSVC, .dcu for Borland C++ and so &#111;n). Then, the linker (which is usually a separate program from the compiler) reads in each of these object files, sticks them together and ties up all the references between them (e.g., if file B.cpp contains a function call to something defined in file A.cpp, then the linker will have to patch that reference up so it refers to the right location in the final executable). The linker adds whatever header information is required by the output format and emits the result, which is the executable that you can then run.<br><br>That explanation is of course incredibly simplified and skips over a huge number of alternative implementations and details, but hopefully it'll help you understand what's going &#111;n.<br><br>John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.

This topic is closed to new replies.

Advertisement