Assume doc.pdf is the target.

The following rule triggers a regeneration of doc.pdf whenever doc.refer is updated, but is also happy when doc.refer does not exist at all:

doc.pdf: doc.mom $(wildcard doc.refer)
    pdfmom -e -k < $< > $@

However the following pattern rule does not accomplish the same (the PDF is generated correctly, but a rebuild is not triggered when changing doc.refer):

%.pdf: %.mom Makefile $(wildcard %.refer)
    pdfmom -e -k < $< > $@

I suspect that the wildcard command is executed before the % character is expanded. How can I work around this?

3 Answers 3


The GNU Make function wildcard takes a shell globbing pattern and expands it to the files matching that pattern. The pattern %.refer does not contain any shell globbing patterns.

You probably want something like

%.pdf: %.mom %.refer
        pdfmom -e -k < $< > $@

%.pdf: %.mom
        pdfmom -e -k < $< > $@

The first target will be invoked for making PDF files when there's a .mom and a .refer file available for the base name of the document. The second target will be invoked when there isn't a .refer file available.

The order of these targets is important.


You maybe able to use Secondary Expansion:

%.pdf: %.mom Makefile $$(wildcard %.refer)
    pdfmom -e -k < $< > $@

GNU Make evaluate all functions in order to generate internal representation of pattern rules.

Thus $(wildcard %.refer) evaluated to empty string for your rule.

The only way to achieve your goal is to exploit rule ordering like Kusalananda's solution suggest (info "(make)Pattern Match"):

10.5.4 How Patterns Match

   It is possible that more than one pattern rule will meet these
criteria.  In that case, 'make' will choose the rule with the shortest
stem (that is, the pattern that matches most specifically).  If more
than one pattern rule has the shortest stem, 'make' will choose the
first one found in the makefile.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .