5

I am currently confused as to how makefile targets work. I have a current understanding, and I don't know if it is correct because the tutorials I've been reading aren't very clear to me. Here is my current understanding

  1. when you run 'make' in the terminal, the makefile utility finds the first target in the makefile and tries to run it, but before doing so it looks at all of the dependencies in the file
  2. (this is where I start getting confused): If the dependency is a target in the makefile, but does not exist as a file in the makefile's directory, make simply runs the target. If the dependency is a file name, but not a target in the makefile, the utility checks for the existance of the file, and if the file doesn't exist, the utility yells at you. If the dependency is a file that exists in the directory AND a target, the target is run provided that any of the files that the file-target depend on are newer than the associated file.

Do I have it down right? Is it simpler than I'm making it out to be?

Oliver Charlesworth
274k34 gold badges591 silver badges687 bronze badges
asked Jun 1, 2013 at 23:29
1
  • That sounds about right. Commented Jun 1, 2013 at 23:31

2 Answers 2

7

You have it right, more or less, but it can be stated a little more clearly. You're right about how make chooses the initial target, except of course if the user specifies a specific target on the make command line then that one is used instead of the first one.

Then make basically implements a recursive algorithm for each target, that works like this:

  1. Find a rule to build that target. If there is no rule to build the target, make fails.
  2. For each prerequisite of the target, run this algorithm with that prerequisite as the target.
  3. If either the target does not exist, or if any prerequisite's modification time is newer than the target's modification time, run the recipe associated with the target. If the recipe fails, (usually) make fails.

That's it! Of course, this hides a number of complex issues: in particular item #1 (finding a rule) can be complex in situations where you have no explicit rule for the target. Also behaviors such as what to do when a rule fails can be modified.

But that's the basic algorithm!

answered Jun 2, 2013 at 15:18
Sign up to request clarification or add additional context in comments.

Comments

2

for the question you asked your understanding is correct !!

If you are still confused have a look at this :: http://www.jfranken.de/homepages/johannes/vortraege/make_inhalt.en.html

once comfortable move to other more complete manuals like the GNU manual for make.

answered Jun 2, 2013 at 3:56

2 Comments

That's a very nice intro to make. I like that it splits make functionality off from compiling; it's very easy to conflate the two.
That's the best resource for learning the basics of make hands-down!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.