Makefile在目标正文中设置全局变量 [英] Makefile set global variable in target body

查看:785
本文介绍了Makefile在目标正文中设置全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过配方设置全局变量,然后在另一个独立配方中引用该变量

I want to set a Global variable through a recipe then reference that variable in another independent recipe

下面的代码是示例代码,它在配方内设置变量,但如果在配方外引用,则变量将保留初始值

The below code is an example code that sets the variable within the recipe but the variable stays with the initial value if referenced outside the recipe

ACTIVE = a

switch:
ifeq ($(ACTIVE),b)
    ACTIVE=$(shell echo 'a')
else
    ACTIVE=$(shell echo 'b')
endif

print:
    $(info acitve = $(ACTIVE))

我知道有一些方法可以将目标特定变量的值广播给从属目标,但这不是我想要的.

I know there are ways to broadcast the value of a target-specific variable to dependent targets, but that's not what I want.

推荐答案

您可以为此使用$(eval ...),尽管这几乎总是一个坏主意.我必须假设您的实际情况要复杂得多,因为有许多更好的方法可以完成示例makefile中实际提供的功能.

You can use $(eval ...) for this, although it's almost always a bad idea. I have to assume that your real situation is much more complicated because there are many better ways to accomplish what you've actually provided in the sample makefile.

switch:
        $(eval ACTIVE=$(if $(filter-out a,$(ACTIVE)),a,b))

这篇关于Makefile在目标正文中设置全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆