Creating and usage example of dependency files in gcc / g++ compiler.
-
In this site I have an example, which shows how to create a dependency file.
The dependency file lists all the files, which were required for compilation
of a c / cpp project.
The -MD option to generate a dependency list.
-
This site also suggests a simple script to search files, which are listed in a
given input file.
script
-
The c project, from the above example, is very simple and contains few files.
Large projects, however, has many files. Each file has its dependency list.
To search in all of those files, it requires to extract those files. Again, on
large project, it is very productive to look only in those file, which were
loaded, to avoid working on a wrong file.
A script, that reads all the dependencies and combines to one sorted and
unedified file :
#!/bin/bash
#array of file suffix names
sfx_a=(h hpp c cc)
#name + process ID
res="~/Junk/search_in_d_list_"$$".txt"
tmp="~/Junk/tmp_"$$".txt"
srt="~/Junk/srt_"$$".txt"
cmd="echo search_in_d_list_start > "$res
eval $cmd
for fpd in `ls *.d`; do
for sfx in "${sfx_a[@]}"; do
#cmd="grep \"\\/[a-z].*\."$sfx"[ \\\]\" "$fpd" > "$tmp
cmd="grep \"\\/[a-zA-Z].*\."$sfx"\" "$fpd" > "$tmp
eval $cmd
cmd="sed -i \"s/^ *//\" "$tmp
eval $cmd
cmd="sed -i \"s/[ ]//\" "$tmp
eval $cmd
cmd="sed -i \"s/\\\\\//\" "$tmp
eval $cmd
cmd="cat "$tmp" >> "$res
eval $cmd
done
done
cmd="grep -v search_in_d_list_start "$res" | sort | uniq > "$srt
eval $cmd
cmd="mv -f "$srt" "$res
eval $cmd
#clean up
cmd="rm "$tmp
eval $cmd
-
A few notes on the bash script:
The result file is stored in the home directory of the user under directory
Junk. You must create it, if it does not exist.
The file name contains the bash process ID
($$)
of the script. This script may
take a while to finish and you may want to start the very same script
on different project concurrently.
If more file suffixes are required, you need to add them to the lookup array:
sfx_a=(h hpp c cc)
-
An improved script, written in PERL is given below. It has better
results in last projects, which I was involved in recently. Its invocation
is one of the following:
#0.rm c_depend.txt
#1.find . -name "*.d" | xargs -n1 | xargs -i= perl ~/bin/c_depend.pl =
#2. foreach x ( `find . -name "*.d"` )
#perl ~/bin/c_depend.pl $x
#end
perl script
|