3

In my C++ code, I want to include many header files that are placed in one folder. how can i include them all at once?

4
  • @ildjarn: that could very well be an answer. Commented Apr 15, 2011 at 7:15
  • 3
    Nine times out of ten, you don't need to include all of those headers in every file. Seriously reconsider whether this is actually necessary at the risk of increasing complexity. Commented Apr 15, 2011 at 7:16
  • isn't there a way to include the whole directory containing header files, in the code? Commented Apr 15, 2011 at 7:18
  • you can however automate the creation of the convenience header in your Makefile, see @edgar.holleis answer on that (but use better include guards) Commented Apr 15, 2011 at 8:04

3 Answers 3

11

Create a header file that includes them all and include that instead.

I.e., I know of no compiler that has this functionality built in, and if one did it would certainly be non-standard functionality.

Sign up to request clarification or add additional context in comments.

1 Comment

Since naming is the key: those "big" headers are generally called convenience headers. For example, you might see them in the Boost library, where generally a optional.hpp file will live right next to the optional folder and include (most of) its content.
4

Execute the following shell commands in the directory which holds your .h files:

rm -f meta.h echo "#ifndef META_H" >> meta.h echo "#define META_H" >> meta.h for h in `ls *.h`; do echo "#include \"$h\"" >> meta.h; done echo "#endif /*META_H*/" >> meta.h 

...and then #include "meta.h" alone.

4 Comments

your include guards are not recommended: C++0x draft standards §17.6.3.3.2 says Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace. The guards should not start with _ or contain two _s.
@Mat : Specifically, two consecutive _s.
@ildjarn: indeed, that was not clear. The standards quote for that is: Each name that contains a double underscore _ _ or begins with an underscore followed by an uppercase letter (2.12) is reserved to the implementation for any use.
So this is a good bash solution so long as you remove the first underscore? Why not edit the solution to fix that?
1

You may wrap them in a helping header file. Having created it once, you can include it everywhere.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.