List Info

Thread: 2 level dependencies in Makefiles




2 level dependencies in Makefiles
country flaguser name
Spain
2007-10-25 10:33:14
Imagine the following Makefile:

***********BEGIN
all: 1.a

1.a: 1.o
        cat 1.o >> 1.a

1.o: 1.c
        cat 1.c >> 1.o
***********END


Then, if I have file "1.c", this is
what happens:


johntokio:~$ make
cat 1.c >> 1.o
cat 1.o >> 1.a

johntokio:~$ ls
1.a  1.c  1.o  Makefile


Nothing new up to this point.

If I now delete "1.o"  and type "make"
again,
this is what happens:

johntokio:~$ rm 1.o
johntokio:~$ make
cat 1.c >> 1.o
cat 1.o >> 1.a

That is, everything is rebuilt.

However I don't want everything to be rebuilt. Even
if the intermediate "1.o" file has changed, the
final
result is going to be the same one.

Is there anyway I can tell gmake about this ?

The idea is to keep ".a" libraries in the CVS
together
with the source code (but NOT the object files also)
and only compile source files when at least one of them
is newer than the CVS library.

Thanks !!


Re: 2 level dependencies in Makefiles
country flaguser name
Sweden
2007-10-25 16:24:38
"bocadillodeatungmail.com" <bocadillodeatungmail.com> wrote:

> Imagine the following Makefile:
> 
> ***********BEGIN
> all: 1.a
> 
> 1.a: 1.o
>         cat 1.o >> 1.a
> 
> 1.o: 1.c
>         cat 1.c >> 1.o
> ***********END

You probably want this Makefile:

***********BEGIN
all: 1.a
 
1.a: 1.o
        cat 1.o >> 1.a
 
1.o: 1.c
        cat 1.c >> 1.o
 
.INTERMEDIATE: 1.o
***********END
 
> However I don't want everything to be rebuilt. Even if
the intermediate
> "1.o" file has changed, the final result is
going to be the same one.
> 
> Is there anyway I can tell gmake about this ?

>From http://www.gnu.org/software/make/manual/make.html
#Chained-Rules

-8<--------------------------
Intermediate files are remade using their rules just like
all other files.
But intermediate files are treated differently in two ways.

The first difference is what happens if the intermediate
file does not
exist. If an ordinary file b does not exist, and make
considers a target
that depends on b, it invariably creates b and then updates
the target
from b. But if b is an intermediate file, then make can
leave well enough
alone. It won't bother updating b, or the ultimate target,
unless some
prerequisite of b is newer than that target or there is some
other reason
to update that target.

The second difference is that if make does create b in order
to update
something else, it deletes b later on after it is no longer
needed.
Therefore, an intermediate file which did not exist before
make also does
not exist after make. make reports the deletion to you by
printing a `rm
-f' command showing which file it is deleting.

Ordinarily, a file cannot be intermediate if it is mentioned
in the
makefile as a target or prerequisite. However, you can
explicitly mark a
file as intermediate by listing it as a prerequisite of the
special target
.INTERMEDIATE. This takes effect even if the file is
mentioned explicitly
in some other way.

You can prevent automatic deletion of an intermediate file
by marking it
as a secondary file. To do this, list it as a prerequisite
of the special
target .SECONDARY. When a file is secondary, make will not
create the file
merely because it does not already exist, but make does not
automatically
delete the file. Marking a file as secondary also marks it
as
intermediate. 
-8<--------------------------


> The idea is to keep ".a" libraries in the CVS
together with the source
> code (but NOT the object files also) and only compile
source files when
> at least one of them is newer than the CVS library.

Yep, .INTERMEDIATE will do that for you.

regards Henrik
-- 
The address in the header is only to prevent spam. My real
address is:
hc1(at)poolhem.se Examples of addresses which go to
spammers:
rootlocalhost postmasterlocalhost


Re: 2 level dependencies in Makefiles
country flaguser name
Spain
2007-10-26 04:55:19
> .INTERMEDIATE: 1.o


That's exactly what I was looking for.

Thank you very much 


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )