Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
1  
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
add comment

3 Answers

up vote 1 down vote accepted

From the root of your tree run

find . -name \*cpp | while read FILE
do
    sed -i '1i #include "rootpath/headerfile.h"' "$FILE"
done
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
add comment

I got hold of this on CLUG-mailing list:

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

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

share|improve this answer
add comment

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
add comment

Your Answer

 
discard

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.