Sign in to follow this  

Including Files

This topic is 3489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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();

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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++ [I think] and so on). 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.

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 on.

John B

Share this post


Link to post
Share on other sites

This topic is 3489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this