简体   繁体   English

如何在Makefile中创建静态隐式规则?

[英]How do I create a static implicit rule in a Makefile?

I originally had two implicit rules(simplified for clarity): 我最初有两个隐式规则(为清楚起见而简化):

%$(EXESUFFIX) : %.c
    $(CC) -o $* $< 

%$(EXESUFFIX) : %.cpp
    $(CXX) -o $* $<

But the problem is that on OS X and Linux $(EXESUFFIX) is blank which leads the rule to match the wrong things. 但是问题是在OS X和Linux上$(EXESUFFIX)是空白的,这导致规则匹配错误的内容。 So I am trying to use the static pattern rule as follows: 因此,我尝试使用静态模式规则 ,如下所示:

$(EXECS) : %$(EXESUFFIX) : %.c
    $(CC) -o $* $< 

$(EXECS) : %$(EXESUFFIX) : %.cpp
    $(CXX) -o $* $<

Where $(EXECS) is the target and therefore devoid of the extension. $(EXECS)是目标,因此没有扩展名。 But now, the top rule is being run for sources which end in .cpp. 但是现在,对以.cpp结尾的源代码运行最高规则。 How do I fix this? 我该如何解决?

For a complete example: 举一个完整的例子:

Makefile: Makefile文件:

EXESUFFIX = 
EXECS = test

$(EXECS) : %$(EXESUFFIX) : %.c
    $(CC) -o $* $< 

$(EXECS) : %$(EXESUFFIX) : %.cpp
    $(CXX) -o $* $<

test.cpp: TEST.CPP:

#include <stdio.h>

int main(int argc, char *argv[]){
  printf("Hello World\n");
  return 0;
}

This prints out the errors: 打印出错误:

Makefile:8: warning: overriding commands for target `test'
Makefile:5: warning: ignoring old commands for target `test'
make: *** No rule to make target `test.c', needed by `test'.  Stop.

The GNU Make manual you linked to is quite clear about the difference between static rules and implicit rules. 您链接到的GNU Make手册非常清楚静态规则和隐式规则之间的区别。

4.12.2 Static Pattern Rules versus Implicit Rules 4.12.2静态模式规则与隐式规则

A static pattern rule has much in common with an implicit rule defined as a pattern rule > (see Defining and Redefining Pattern Rules). 静态模式规则与定义为模式规则>的隐式规则有很多共同点(请参阅定义和重新定义模式规则)。 Both have a pattern for the target and patterns for constructing the names of prerequisites. 两者都有目标的模式和构造先决条件名称的模式。 The difference is in how make decides when the rule applies. 区别在于make如何确定何时应用规则。

An implicit rule can apply to any target that matches its pattern, but it does apply only when the target has no recipe otherwise specified, and only when the prerequisites can be found. 隐式规则可以应用于与其模式匹配的任何目标,但是仅当目标没有另外指定配方时,并且仅在可以找到前提条件时,它才适用。 If more than one implicit rule appears applicable, only one applies; 如果出现多个不适用的隐含规则,则仅适用一个。 the choice depends on the order of rules. 选择取决于规则的顺序。

By contrast, a static pattern rule applies to the precise list of targets that you specify in the rule. 相反,静态模式规则适用于您在规则中指定的精确目标列表。 It cannot apply to any other target and it invariably does apply to each of the targets specified. 它不能应用于任何其他目标,并且总是适用于指定的每个目标。 If two conflicting rules apply, and both have recipes, that's an error. 如果两个冲突的规则适用,并且都有配方,那就是错误的。

I suggest splitting your executables between C and C++ programs and defining independent rules for each. 我建议在C和C ++程序之间拆分可执行文件,并为每个程序定义独立的规则。

I cannot reproduce your error, but this works in GNUMake 3.81: 我无法重现您的错误,但这在GNUMake 3.81中有效:

%$(EXESUFFIX) : %.c
    $(CC) -o $* $<

%$(EXESUFFIX) : %.cpp
    $(CXX) -o $* $<

Having two different rules that fit the same target is legal with ordinary pattern rules, but not with static pattern rules. 具有两个适合同一目标的不同规则对于普通模式规则是合法的,但对于静态模式规则则不合法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM