开发者

How to generate top level include file

How to generate top level include file which includes all other header files from all subdirectories?

I am working on a large project with many subdirectories and many header files in those subd开发者_开发问答irectories. Subdirectories contain more subdirectories and those subdirectories have more header files. It is a big tree like multi-level structure. I want to provide top level header file to the users who doesn't want to include headers manually. Top level header file should include all other headers with relative path. I am sinking to automate the process with sed or awk, but not sure how to start.

Any tips are greatly appreciated.

Thanks.


find . -name '*.h' | sed -e 's/\.\///' -e 's/^/#include "/' -e 's/$/"/' >> master.h

  • Find all .h files
  • strip the leading "./"
  • add #include " at the beginning of line
  • add " at the end of line
  • redirect to master.h

But I'm not sure this is such a good idea.

I'd rather include only the necessary headers and keep the number of headers needed as small as possible.


You're on the right track. Personally I'd use find and just pipe the output to a file. Something like:

 find . -name "*.h" -print > myBigHeader.h

Also, you'll want to deal with dependency generation in your makefile so that if one of those headers get's changed make will be able to recompile the objects that depend on it.


The other answers tell you how to do this and will work well, but there are some potential problems with this approach:

  • compile time may be significantly increased as more headers need to read in and processed.

  • a change to any header file will mean all code using your global header file would be rebuilt, even if it didn't actually use the parts that were changed.

  • makes it harder to see the project as a collection of smaller problems and sub-systems.

  • potential for function/variable name clashes and shadowing (especially in C with no namespaces to limit the scope)

A better solution is to organise your project properly. Break it down into sub-systems and libraries which have headers exposing a useful limited interface to the rest of the system, whilst keeping the implementation details internal to the library/sub-system/module.


echo "#ifndef INCLUDEALLH" > includeall.h
echo "#define INCLUDEALLH" >> includeall.h
find . -name \*.h | sed 's,^[.]/,,' | while read line; do echo "#include \"$line\""; done >> includeall.h
echo "#endif" >> includeall.h
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜