How can I query prerequisites of a target from a GNU Make makefile?

E.g. if the targets specified like this:

fred: wilma barney betty

I want to print prerequisites of fred like this:

$ make some_action fred
wilma barney betty
  • This doesn't answer your question, but have you considered alternative to GNU Make. I use SCons, which is good for small programs at least. With larger ones it supposedly has performance problems. If you know Python, then that is an even better reason to use it, since SCons build recipes are written in Python. – Faheem Mitha Sep 26 '11 at 15:21

Maybe a dry run is what you are looking for?

make --dry-run fred

The man page gives us this about the dry run:

-n, --just-print, --dry-run, --recon
  Print the commands that would be  executed,  but  do  not 
  execute them.

So it will not give you a direct list, but a indirect list since you know that he would like to execute what he depends on.


Another possibility, though one that gives you tons more info than you want, is the "print database" option. From the manual:

      Print the data base (rules and variable values) that results from reading the
      makefiles; then execute as usual or as otherwise specified. This also prints the
      version information given by the ‘-v’ switch (see below). To print the data base
      without trying to remake any files, use ‘make -qp’. To print the data base of
      predefined rules and variables, use ‘make -p -f /dev/null’. The data base output
      contains file name and line number information for recipe and variable
      definitions, so it can be a useful debugging tool in complex environments.

Fair warning, though: sifting through this mess will be brutal without some post-processing.


Let's say your Makefile is this:

.PHONY: wilma barney betty fred: wilma barney betty

The latest version of remake has a --profile option which outputs data that can be turned into a graph of the dependencies checked. The data file output format is what callgrind uses, so another program is needed to produce the graphs from the data that is output. See https://github.com/rocky/remake/tree/master/profile

In an older version of remake, here is how you could get the dependencies using

$ remake -X -f Makefile
GNU Make 4.1+dbg0.91
Built for x86_64-unknown-linux-gnu
Copyright (C) 1988-2014 Free Software Foundation, Inc.
Copyright (C) 2015 Rocky Bernstein.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Reading makefiles...
Updating makefiles....
Updating goal targets....
 File 'fred' does not exist.
   File 'wilma' does not exist.
  Must remake target 'wilma'.
  Successfully remade target file 'wilma'.
<- (/tmp/??:0)
remake<0> target fred depend

fred: wilma barney betty
remake<1> quit
remake: That's all, folks...

If you put those commands issued into a file, say cmds.txt:

target fred depend quit

Then you can run:

cat cmds.txt | remake -X -f Makefile

I leave as an exercise how to do something more elaborate such as writing program that takes a dependency name and runs the above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.