Re: makefile and header files
On Jul 12, 11:40 am, Michael DOUBEZ <michael.dou...@free.fr> wrote:
HT-Lab a =E9crit :
Not strictly a C++ question but I am sure this is a common compile issu=
e :-)
I have a generic makefile (see below) that I would like update such tha=
t if
I make some changes to a header file the associated cpp file is recompi=
led.
I tried using constructs like:
.h.cpp:
touch $<
thinking that if make detects a change in file.h it will touch file.cpp
which will then result in .cpp.o rule being executed. I tried this and =
some
other permutations but they all failed.
Any idea how I can fix this issue?
No but if all you need is recompilation because of header change, the
usual method is to add the rule:
monobjet.o: monheader.h myheader.h meinheader.h
Using gcc, you can automatically generate dependency files through the
-M (or -MM) flags (man gcc for more info).
It's not a question of the compiler; it's a question of make (or
rather, the two together) It's almost always possible to hack
something that works, but it is rarely portable. Sun's make and
Sun CC are probably the simplest; the compiler can be told to
generate the dependencies while compiling, and a special target
in the makefile tells the makefile to use them. GNU make
documents how to build dependency files which it will use, using
compiler output from a special compiler pass; under Unix, -M is
a more or less standard way of telling the compiler to generate
dependencies (although it's -xM1 with Sun CC). There's no
guarantee that what the compiler generates will be compatible
with whatever make you're using, but they're usually close
enough that with a little help from sed...
I use GNU make exclusively, originally because make is even less
portable the C++, and GNU make is about the only one available
everywhere. And I've always managed to get it to generate
dependency files automatically. With VC++, of course, it takes
a bit more work, but the following script does the trick:
stripName () {
expr ` basename "$1" ` : '\(.*\)\.[^.][^.]*'
}
cppFlags="$1"
shift
for fn in $*
do
objectName=` stripName "${fn}" `.obj
# echo "${objectName} : ${fn}"
cl /E ${cppFlags} /Tp ${fn} |
sed -n 's:^ *# *line *[0-9]* *"\(.*\)".*:\1:p' |
sed 's:\\\\:/:g' |
egrep -v '/Microsoft *.*/VC/' |
egrep -v '/Program Files/' |
sort -u |
sed "s|^|${objectName} : |"
done
(Obviously, you'll have to have a Unix compatible tool kit
installed for this to work:-). But does anyone really use the
command interpreter which comes installed with Windows?)
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34