How to write a Makefile rule to download a file only if it is missing?

I am trying to write a Makefile that should load some sources if and only if they are missing.

Sort of:

hello: hello.c
    gcc -o hello hello.c

hello.c:
    wget -O hello.c http://example.org/hello.c

But of course, this causes a load hello.cevery time the command is run. I would like to hello.cbe loaded with this Makefile only if it is missing. Is this possible with GNU make, and how to do it, if so?

+5
source share
3 answers

, wget hello.c, . make , hello.c .

hello.c:
        wget ...
        touch $@

EDIT: -N wget wget -, ( , .)

+8

Makefile hello.c, . , - ? . :

hello: hello.c
        gcc -o hello hello.c

hello.c:
        echo 'int main() {}' > hello.c

% make
echo 'int main() {}' > hello.c
gcc -o hello hello.c
% rm hello
% make
gcc -o hello hello.c
% rm hello*
% make
echo 'int main() {}' > hello.c
gcc -o hello hello.c

( echo )

+6

Makefile , , :

1) , .PHONY, .

2) Make sure the name of the target source matches the file you are downloading.

You can also try running make -dto see why make thinks he needs to "rebuild" the source file.

+6
source

All Articles