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?
- @ildjarn: that could very well be an answer.Mat– Mat2011-04-15 07:15:03 +00:00Commented Apr 15, 2011 at 7:15
- 3Nine 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.Cody Gray– Cody Gray ♦2011-04-15 07:16:49 +00:00Commented Apr 15, 2011 at 7:16
- isn't there a way to include the whole directory containing header files, in the code?Aspirant Developer– Aspirant Developer2011-04-15 07:18:07 +00:00Commented 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)Matthieu M.– Matthieu M.2011-04-15 08:04:58 +00:00Commented Apr 15, 2011 at 8:04
Add a comment |
3 Answers
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.
1 Comment
Matthieu M.
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.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
Mat
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.ildjarn
@Mat : Specifically, two consecutive
_s.Mat
@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.Elliott
So this is a good bash solution so long as you remove the first underscore? Why not edit the solution to fix that?