1

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

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

One invocation of sed can take multiple -e options (processed in order).
4

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.

1 Comment

Top level header is for convenience only. I see many project provide such a deader file.
4

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.

Comments

2
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.