简体   繁体   English

如何在目标Makefile中设置多个变量

[英]How to set multiple variables in target makefile

I have one problem. 我有一个问题。 I cannot set multiple variables depending on target. 我不能根据目标设置多个变量。 I need to set two variables for example if makefile has been run with option cpp (target), two variables should be set,but I get only one variable (CC) set. 我需要设置两个变量,例如,如果makefile已使用选项cpp(目标)运行,则应设置两个变量,但我只能设置一个变量(CC)。 But another is left as default. 但是另一个保留为默认值。

cpp:EXTENSION=cpp
cpp:CC=g++
cpp:COMPILE

Please help with this problem. 请协助解决这个问题。

I cannot set multiple variables depending on target. 我不能根据目标设置多个变量。

In GNU Make , this is typically achieved by analyzing the content of the $(MAKECMDGOALS) variable and then setting the variables depending on the specified goals. GNU Make中 ,这通常是通过分析$(MAKECMDGOALS)变量的内容,然后根据指定的目标设置变量来实现的。

For example, a Makefile: 例如,一个Makefile:

ifeq ($(filter cpp,$(MAKECMDGOALS)),cpp)
MSG=make cpp was called!
endif

ifeq ($(filter notcpp,$(MAKECMDGOALS)),notcpp)
MSG=make notcpp was called!
endif

all:
        @echo possible targets: cpp, notcpp; false

notcpp cpp:
        @echo $(MSG)

Running the make cpp would print make cpp was called! 运行make cpp会打印出make cpp was called! .

Running the make notcpp would print make notcpp was called! 运行make notcpp将打印make notcpp was called! .

(Explanation of the ifeq ($(filter cpp,$(MAKECMDGOALS)),cpp) construct. The $(filter) function looks for the word (first argument, cpp ) in the list (second argument, $(MAKECMDGOALS) ). If it finds the word, it returns it. Otherwise, it returns an empty string. The ifeq then compares the result of the $(filter) function to the sought word: if equal, then the block til endif is parsed/executed. Alternatively, one could write ifneq ($(filter cpp,$(MAKECMDGOALS)),) to test that the result of $(filter) is not an empty string. For more info, see $(filter) and ifeq in GNU Make documentation. Additionally, about general function syntax and how to call a custom function .) (解释ifeq ($(filter cpp,$(MAKECMDGOALS)),cpp)构造。 $(filter)函数在列表中查找单词(第一个参数cpp )(第二个参数$(MAKECMDGOALS) )。如果找到该单词,则返回该单词;否则,返回一个空字符串;然后, ifeq$(filter)函数的结果与所需单词进行比较:如果相等,则将解析/执行直到endif的块。 ,可以编写ifneq ($(filter cpp,$(MAKECMDGOALS)),)来测试$(filter)的结果不是空字符串。有关更多信息,请参见GNU Make文档中的$(filter)ifeq 。另外,关于常规函数语法以及如何调用自定义函数 。)

Another possibility is using multiple Makefiles. 另一种可能是使用多个Makefile。 In the main Makefile, depending on the target you pass the control to the target-specific Makefile, eg: 在主Makefile中,根据目标,将控件传递给特定于目标的Makefile,例如:

cpp notcpp:
    $(MAKE) -f Makefile.$@ all

Use of target-specific includes is also a popular option. 使用特定于目标的包含也是一种流行的选择。

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

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