Unix Programming - makefile problem: multiple targets in single rule

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > November 2007 > makefile problem: multiple targets in single rule





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author makefile problem: multiple targets in single rule
Morfys

2007-11-17, 1:33 pm

Hi,

I'm having a makefile problem when multiple files appear as
dependencies and targets in rules.

For example, let's say that only the file d intially exists and
"myprog d" generates b and c.

If I have the following in my makefile:

a: b c
otherprog
b c: d
myprog d

Running "make -n", will show the following:
myprog d
myprog d
otherprog

as "make" traverses the dependency tree from left to right depth first
it seems.

Is there any way to make "make" run "myprog d" a single time instead
of twice (and at the same time not call "myprog d" unnecessarily if b
and c already exist and haven't bee modified)?

The following hack isn't suitable:
a: b
b: c
otherprog
c: d
myprog d

This is because myprog will always at least "touch" b and c, and thus
"myprog d" will always execute, even if b and c hadn't be modified.

Thanks for any help.
toby

2007-11-17, 7:26 pm

On Nov 17, 5:00 pm, Morfys <morfyss...@gmail.com> wrote:
> Hi,
>
> I'm having a makefile problem when multiple files appear as
> dependencies and targets in rules.
>
> For example, let's say that only the file d intially exists and
> "myprog d" generates b and c.
>
> If I have the following in my makefile:
>
> a: b c
> otherprog
> b c: d
> myprog d
>
> Running "make -n", will show the following:
> myprog d
> myprog d
> otherprog


It looks like make is smart enough to re-check the existence of both
targets anyway - this seems to work correctly for me:

$ make clean
rm -f b c

$ make -n
cp d b; cp d c
cp d b; cp d c
ls -l b c

$ make
cp d b; cp d c
ls -l b c
-rw-r--r-- 1 toby toby 0 Nov 17 19:56 b
-rw-r--r-- 1 toby toby 0 Nov 17 19:56 c

$ cat Makefile
myprog=cp d b; cp d c
a: b c
ls -l b c
b c: d
$(myprog)
clean : ; rm -f b c


>
> as "make" traverses the dependency tree from left to right depth first
> it seems.
>
> Is there any way to make "make" run "myprog d" a single time instead
> of twice (and at the same time not call "myprog d" unnecessarily if b
> and c already exist and haven't bee modified)?
>
> The following hack isn't suitable:
> a: b
> b: c
> otherprog
> c: d
> myprog d
>
> This is because myprog will always at least "touch" b and c, and thus
> "myprog d" will always execute, even if b and c hadn't be modified.
>
> Thanks for any help.


Morfys

2007-11-18, 1:35 am

thank you very much. I was fooled by the output of "make -n".

In your example,
make -n

shows:

cp d b; cp d c
cp d b; cp d c
ls -l b c

where as, in reality running:
make

causes only the following to execute:
cp d b; cp d c
ls -l b c
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com