user3816949
user3816949

Reputation: 11

Makefiles, targets as dependencies

I'm new to Makefiles and I'm trying to debug something that extends over multiple Makefiles.

target2: target1

command 1

Target 1 builds the source code (takes about ~4 hours). Target 2 is simply supposed to copy resulting files into certain directories.

I've already made target 1. When I immediately try to make target 2, it seems to think it is missing some files/libraries and rebuilds for ~5-10 mins. The files have not been moved or changed. I can executed target 2 immediately after target 1 and it will ALWAYS rebuild for a few minutes.

Target 1 and command 1 are creating the same directory. Is it possible that the timestamp on this directory is causing the rebuilding of these libraries? Or is it only files that will trigger rebuilding with timestamp discrepancy?

Upvotes: 1

Views: 2284

Answers (2)

themeasure43
themeasure43

Reputation: 51

See, GNU make treats all target names as file names by default, so that you can generate some file from the list of prerequisites, like in example below the main.o object file will be generated by compiling main.c with main.h

main.o : main.c defs.h
    gcc -c main.c

However this is not always what you want to do, sometimes you want to have a target named foo that does something that's need to be done prior to execution of target bar, this is totally ok except you need to tell make that foo is not a file name so that it will not attempt to generate file named foo. What you need here is called a .PHONY target. A phony target is one that is not really the name of a file; rather it is just a name for a recipe to be executed when you make an explicit request. GNU Make Manual on Phony Targets

.PHONY: foo
   foo: bar

In the example above make is told not to treat target foo as a filename and therefore not to attempt to generate the file named foo.

In your case the target1 may depend on a target that has no recipe or you may have a file that has the same name as target1 so that make is always attempting to build this file instead (in this case you need a .PHONY target), or eventually you may have timestamps or object files somehow altered after target1 is built so that make considers it needs to be rebuilt anew.

Also, in your target1 you may be building something that make cannot analyze, thus being unable to tell if it is already built; this includes working with certain types of archives, generating or transforming text, calling another build systems (like e.g. ant) in subroutines, etc.

Upvotes: 1

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136208

Targets are actually filenames. The recipes (commands) must create or update that target file otherwise make considers that target missing or out of date and will try to rebuild it on the next run; unless it is a phony target which gets rebuilt every time.

Invoke make with -d option to see which targets it is building and why.

Upvotes: 3

Related Questions