Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a sub-dir tree with varying number of branches and most of the branches contain .cpp files (many of them). I have a header-file at the root of the tree that I want to have as a

#include "<constructed-relative-path-to-root>/headerfile.h"

as the first line of each .cpp.

an alternative to constructed-relative-path-to-root would be the hard-coded path, which would have to be adjusted whenever the project is relocated)

a second alternative would be to copy the content of the header-file in at the top of each .cpp file

I have no idea how to write such a script. Can anyone please help?

share|improve this question
Can't you just add the root to your compiler's include path and use #include <headerfile.h> everywhere? – Jim Paris Nov 28 '12 at 19:12
@JimParis: Yes, that will also work; the problem remains to add the line #include <headerfile.h> in each .cpp in all the sub-dirs - there are hundreds - This would be solved by Doug O'Neal's answer :) – slashmais Nov 29 '12 at 5:54
If you're using GCC, you could go one step further and use -include headerfile.h on the command line. No source code changes at all! – Jim Paris Nov 29 '12 at 6:47
up vote 1 down vote accepted

From the root of your tree run

find . -name \*cpp | while read FILE
    sed -i '1i #include "rootpath/headerfile.h"' "$FILE"
share|improve this answer
That will work for the first alternative, and can be expanded to pick up the pwd automatically. thx – slashmais Nov 28 '12 at 13:00
Adjusting your answer to Jim Paris' comment would give the cleanest solution. – slashmais Nov 29 '12 at 6:01

I got hold of this on CLUG-mailing list:

find . -type f -iname '*.cpp' | while read f ; do
   curr=$(dirname $f)
   relpath=$(python -c "import os.path; print os.path.relpath('$base',
   sed -i -e '1i#include "'$relpath/headerfile.h'"' "$f"

Copy the above code into a file, make it executable and run from commandline.

share|improve this answer

If your goal is to include a header file at the start of every source file you build, and you're using gcc, you don't need to change your files at all; you can simply add:

-include headerfile.h

to the gcc command line. From the man page:

   -include file
       Process file as if "#include "file"" appeared as the first line of
       the primary source file.  However, the first directory searched for
       file is the preprocessor's working directory instead of the
       directory containing the main source file.  If not found there, it
       is searched for in the remainder of the "#include "..."" search
       chain as normal.
share|improve this answer
I was not aware of this - very handy to test stuff before making changes fixed for porting to other platforms. – slashmais Nov 29 '12 at 7:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.