24

我有一堆使用相同类型的 make 规则构建的应用程序:

apps = foo bar baz

all: $(apps)

foo: foo.o $(objects)
    $(link)

bar: bar.o $(objects)
    $(link)

baz: baz.o $(objects)
    $(link)

如果他们有扩展名(例如.x),我可以制定一个模式规则,例如:

%.x: %.o $(objects)
    $(link)

而且我不必为每个应用程序编写新规则。

但他们没有扩展名,我很确定:

%: %.o $(objects)
    $(link)

将不起作用(因为它指定要构建可以使用此规则的任何文件)。

无论如何要指定一个涵盖所有$(apps)构建规则的规则?

4

3 回答 3

29

这看起来像是静态模式规则的工作:

$(apps) : % : %.o $(objects)
    $(link)
于 2013-03-30T12:49:56.403 回答
8
%: %.o $(objects)
    $(link)

以上应该工作。

You can limit the scope of the rule by converting it into a static pattern rule, so that it is only considered for your list of targets:

$(apps) : % : %.o $(objects) # only consider this for $(apps) targets
    $(link)
于 2013-03-30T12:51:21.717 回答
0

not an answer to what you are looking for , but a reason that might explain why such level of generic code might not yield good results. ....

static patterns rely on the presence of a stem to match and build a dependency chain. pretty much in the same manner as the implicit rules (which are used for targets that dont have any recipie.)

i see what you were trying to achive , making a generic rule that will satisfy all the target checks for object and link in your code.

something like this ::

 % : % : $(rule1)
         echo / generic code ;

so that it gets invoked for all apps in differnt scenarios

since you dont want to add a extension (this becomes the root of some issues ) the issue with this is that the target will get reflected in the dependency as well since there will be no way of differentiating the dependencies form the targets.

hence if you did try that i think you will get here ...

 $ make -nf mk.t
   mk.t:18: *** mixed implicit and static pattern rules.  Stop.

:) , i will give this a try again tomorrow to see if i can get this working in a real generic way. Nice question though.

于 2013-03-30T20:39:38.243 回答