Click here to Skip to main content
15,867,308 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
Posted
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 :
C++
#ifndef _SHLOBJ_H_
#include <ShlObj.h>
#endif
 
Share this answer
 
The solution is generally to use conditional includes.
MyFile.H
C++
#ifndef _MY_FILE
#define _MY_FILE

... the included headers ...

#endif // _MY_FILE

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

... MyFile definitions ...

#endif /* _MY_FILE */

And the order becomes irrelevant.
 
Share this answer
 
Comments
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.

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