C++ 包含目录中的所有文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3061582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
include all files in a directory?
提问by Karl Glaser
How can one achieve what the following code is trying to do?
如何实现以下代码尝试执行的操作?
#include "dir/*"
采纳答案by el.pescado
In bash:
在 bash 中:
HEADER=all_headers.h
echo "#ifndef __ALL_HEADERS__" > $HEADER
echo "#define __ALL_HEADERS__" >> $HEADER
for file in dir/*.h
do
echo "#include <$file>" >> $HEADER
done
echo "#endif" >> $HEADER
回答by Bj?rn Pollex
One way to achieve that is to write a convenience header that includes all the headers you want. Keep in mind that including headers you will not use may unnecessarily increase compilation time.
实现这一目标的一种方法是编写一个方便的标头,其中包含您想要的所有标头。请记住,包含您不会使用的头文件可能会不必要地增加编译时间。
回答by Jordan Lewis
You can't, without running a script beforehand that generates all #include statements.
如果不事先运行生成所有 #include 语句的脚本,就不能。
The preprocessor can only handle one file per #include statement, so it requires an actual #include for every single file you wish to be included in preprocessing.
预处理器每个#include 语句只能处理一个文件,因此对于您希望包含在预处理中的每个文件,它都需要一个实际的#include。
回答by ezpz
Look at how Boostdoes this for, say, utility.hpp
.
看看Boost如何做到这一点,例如,utility.hpp
。
$ cat /usr/include/boost/utility.hpp
// Boost utility.hpp header file -------------------------------------------//
<snip>
#ifndef BOOST_UTILITY_HPP
#define BOOST_UTILITY_HPP
#include <boost/utility/addressof.hpp>
#include <boost/utility/base_from_member.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/checked_delete.hpp>
#include <boost/next_prior.hpp>
#include <boost/noncopyable.hpp>
#endif // BOOST_UTILITY_HPP
Now you can just use #include <boost/utility.hpp>
.
现在你可以使用#include <boost/utility.hpp>
.