Eranna Yadav
Eranna Yadav

Reputation: 31

Why does Makefile re-compile when source file copied from other directory

My Makefile has

all: depends hello.o

depends:
    cp other_dir/hello.c  ./hello.c

hello.o: hello.c
    cc -c $< -o $@

Why it's compiling object file hello.o everytime when i copy source file from other_dir to present working directory

Upvotes: 1

Views: 52

Answers (1)

user9706
user9706

Reputation:

Your last rule says build hello.o from hello.c when the latter is newer than the former. When you copy hello.c from depends/ it will update timestamp on hello.c and hence considered newer.

$ stat hello.c|grep Modif
Modify: 2022-12-29 23:56:31.041115631 -0500
$ make
cp other_dir/hello.c  ./hello.c
cc    -c -o hello.o hello.c
$ stat hello.c|grep Modif
Modify: 2022-12-29 23:56:42.225075833 -0500

You can ask cp, with the option --preserve=timestamp to not update timestamp when it's being copied:

$ make
cp --preserve=timestamp other_dir/hello.c  ./hello.c

Upvotes: 1

Related Questions