include all files in a directory?

2019-01-18 09:41发布

How can one achieve what the following code is trying to do?

#include "dir/*"

4条回答
放荡不羁爱自由
2楼-- · 2019-01-18 09:50

Look at how Boost does this for, say, 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>.

查看更多
The star\"
3楼-- · 2019-01-18 09:57

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.

查看更多
Bombasti
4楼-- · 2019-01-18 09:58

You can't, without running a script beforehand that generates all #include statements.

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.

查看更多
Fickle 薄情
5楼-- · 2019-01-18 10:02

In 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
查看更多
登录 后发表回答