include all files in a directory?

前端 未结 4 1912
别跟我提以往
别跟我提以往 2020-12-09 15:30

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

#include \"dir/*\"
相关标签:
4条回答
  • 2020-12-09 16:22

    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.

    0 讨论(0)
  • 2020-12-09 16:25

    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>.

    0 讨论(0)
  • 2020-12-09 16:30

    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
    
    0 讨论(0)
  • 2020-12-09 16:30

    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.

    0 讨论(0)
提交回复
热议问题