Create rule in makefile for just a set of files

dividebyzero picture dividebyzero · Oct 7, 2010 · Viewed 7.6k times · Source

I am writing a Makefile, and I want to use a generic rule with wildcards, like

%: bkp/%
    cp $< $@

But I wanted this rule to be valid only for a few specific files. I wanted to define a variable with the list, for example

file_list = foo.c bar.c zzz.c

and configure the rule so it is only valid for files that are listed in this variable. How do I do that?

Answer

j_random_hacker picture j_random_hacker · Oct 7, 2010

You want a static pattern rule:

file_list = foo.c bar.c zzz.c

$(file_list): %: bkp/%
        cp $< $@

The syntax is very similar to the implicit pattern rule you were using. And yes, it's generally safer (more predictable).