Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create rule in makefile for just a set of files

Tags:

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?

like image 420
dividebyzero Avatar asked Oct 07 '10 03:10

dividebyzero


People also ask

How do I link my makefile to .a files?

$(CC) $(CFLAGS) -I$(LIB_PATH) -L$(LIB_PATH) -o $(PROGRAM) main. c -l$(LIB) `pkg-config ...` Basically, you need set the include path to the . h file with -I, then -L for the lib path and -l to set the lib name.

What is $@ in makefile?

$@ is the name of the target being generated, and $< the first prerequisite (usually a source file). You can find a list of all these special variables in the GNU Make manual.

What is a target and a rule in makefile?

A rule appears in the makefile and says when and how to remake certain files, called the rule's targets (most often only one per rule). It lists the other files that are the prerequisites of the target, and the recipe to use to create or update the target.

What is pattern rule in makefile?

A pattern rule looks like an ordinary rule, except that its target contains the character ' % ' (exactly one of them). The target is considered a pattern for matching file names; the ' % ' can match any nonempty substring, while other characters match only themselves.


1 Answers

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).

like image 80
j_random_hacker Avatar answered Sep 21 '22 17:09

j_random_hacker