Click here to Skip to main content
15,399,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the problem of determining the header files required to be included in each source and the problem of determining the order they need to be included The problem is each header file containing only declarations of similarly related types and methods finds types and methods declared in other header files to be helpful These other files also find yet other types and methods useful to them Determining the required headers and their include order is turning out to be a long drawn out tedious affair hopefully w/ no circular dependency but so far so good on that account So my question is how can such a problem be solved preferably avoided so the method I am using to wit compile source w/ only its' own header included from the compiler errors determine what other header files are required include those files repeat as above ad nauseum until a successful compile The only solution I have found via internet search is to declare only the types and methods required in order to avoid including an entire header file though this disagrees w/ "Google C++ Style Guide" However I find this approach distasteful though I am not certain why Is there another solution to avoid such a mess Thank You Kindly My fantasy would be an application which examined each source and which knew of all header files and would then generate a list of the required headers and in the correct order I am a bit surprised no such application seems available I would be happy to write such myself if this is a common problem which would help other programmers and not only my own ignorant self

What I have tried:

Have searched internet but found little discussion and only one solution not well regarded
Updated 13-Nov-21 20:29pm

I go through a similar exercise every time I had a new source code module to a project. It's a lot easier now since I have sorted out all of my headers so that they are essentially "self-contained." By that I mean if I need to use a class I can include its header file and it will include everything needed to compile a module with it. I think that is really the key - the headers themselves have to be sorted out properly so it becomes a simple matter to just include it for a class or function you need to use and move on.

In my opinion, a header file should not require other files to be included in addition to it. I think it should include everything needed to compile itself and do so in a well-behaved way. What that means to me is if it includes a file that uses #pragma once then it is well behaved. If it does not then I use its include guard to include it only if necessary, meaning only if not already included. I use Visual C++ and most of its headers use the pragma but not all do. Here's an example with a file that does not use the pragma :
#ifndef _SHLOBJ_H_
#include <ShlObj.h>
The solution is generally to use conditional includes.
#ifndef _MY_FILE
#define _MY_FILE

... the included headers ...

#endif // _MY_FILE

This means that you can set this in your include files:
#ifndef _MY_FILE
#define _MY_FILE
#include "MyOtherFile.h"
#endif // _MY_OTHER_FILE

... MyFile definitions ...

#endif /* _MY_FILE */

And the order becomes irrelevant.
Richard MacCutchan 14-Nov-21 3:02am
I just use #pragma once in all my headers.
OriginalGriff 14-Nov-21 3:20am
It's not standard, so it isn't available in all compilers, unfortunately.
Richard MacCutchan 14-Nov-21 3:28am
Of course, such a while since I did any serious UNIX/Linux development.
OriginalGriff 14-Nov-21 4:11am
There are still a bunch of Turbo C++ learners here! :OMG:

I suspect because it's easy to pirate cheap and needs very low machine specs.
Richard MacCutchan 14-Nov-21 4:16am
Yes, I keep forgetting the 21st century is still in its infancy.
Rick York 14-Nov-21 13:11pm
This page lists all compilers that support it and there is only one on the list that does not : the IAR compiler.
PaltryProgrammer 14-Nov-21 6:55am
As near as I understand the solution being proposed here is for each header file to include the header files needed so the source file need only include its' own header and Voila the problem is solved I have been avoiding this method for philosophical reasons and only include'ing into source but of course will attempt it now Thank You Kindly
Rick York 14-Nov-21 13:18pm
That is interesting. I began doing things this way for purely philosophical reasons. One of those philosophies being that not every source code file needs its own header and I now go out of my way to make sure they do not. I have become a huge fan of opaque pointer interfaces and it has simplified things A LOT for me. As an example, I write mostly code for windows and the vast majority of dialogs I use do not have a header file. Since they nearly always are editing or just displaying a batch of data, I declare a prototype function to edit or display the data with the header that defines the data. The internal details of the dialog are not needed by any other code so there is no need for it to have a header file. I take a similar approach to most things in an application and this has simplified things for me immensely.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900