PDA

Click to See Complete Forum and Search --> : Standard way to structure a multi-header-file & multi-source-file project


HarryW
Feb 14th, 2001, 09:42 AM
I'm used to seeing header files included in source files, and to see header files included in header files. However, when you have lots of source files, it seems more sensible to include the source files in one main file, with all the headers included at the top, rather than including all the header file info at the top of every source file.

I've heard of people doing this, but it just seems really odd that you would #include source files. Seems to be missing the point somehow.

Is there a standard way to do this? I just started wondering about it again since you generally add the #ifndef, #define, #endif around header files to stop them being included more than once. What if you have the headers included in more than one source file? Perhaps it doesn't matter because the compiler/linker (can't remember which) sorts out the project, since if the code has already been included in another file you don't need to include it again in the other file.

:confused:

Anyone care to comment on standard ways of going about this whole structuring malarkey?

parksie
Feb 14th, 2001, 09:53 AM
Visual C++ seems to sort it all out itself, and you can do similar things using Makefiles. Basically, I use the PCH features of VC++, so I have one file ("project.h") that includes everything that's needed, then ALL files include that. It speeds up compilation by about 5 times :)

Also, if you have any headers included, the linker sorts it all out...you just use something like

cl file1.cpp file2.cpp file3.cpp

...and it will automatically take the headers.

HarryW
Feb 14th, 2001, 09:56 AM
So, in your main project.h file, do you have the #ifndef etc? I'm guessing yes. Is that what makes it faster? Sorry, I didn't quite get what you meant when you said something made it faster to compile.

parksie
Feb 14th, 2001, 09:58 AM
Okay, with VC++ you have a single header file, which contains #includes for everything you need that's EXTERNAL to your project. You have a corresponding .cpp file, which ONLY has one line

#include "file.h"

(like the stdafx.h file)

In Project Settings you set this as a PCH file, and anything else that includes it, it doesn't need to process the include files again, just using the PCH (precompiled headers).

...I'm not making sense, am I? :rolleyes:

HarryW
Feb 14th, 2001, 10:13 AM
Ahhh precompiled headers, I didn't know what PCH was :rolleyes: I don't really know what they do to be honest.

You're so very nearly making sense, I understand everthing you said except - which file do the others have to include? The main header file or the main source file that's including the main header file (the PCH file as you said it was)?

I think what's confusing me is that this source file is a precompiled header file. So does that make it a source or header file? Or.. perhaps it means that the headers it includes are precompiled, and the source file itself isn't.