【问题标题】:Makefile if-statement not evaluating with export variables?Makefile if 语句不使用导出变量进行评估?
【发布时间】:2011-06-14 22:04:53
【问题描述】:

这是我的另一个 question 的一个分支。

为了使用导出变量,我需要做些什么特别的事情吗?

所以我从一个包含另一个位置的主 Makefile 的 Makefile 运行它。在这个本地 Makefile 中,我有:

include /path/to/master/Makefile/
export TOOL = A

现在我的主 Makefile 中有:

ifeq ( $(TOOL), A )
    echo "Tool A will be run..."
    [syntax for toolA]
else
    echo "Tool B will be run..."
    [syntax for toolB]
endif

但是当我运行 gmake 时,toolB 总是在运行!我检查了变量 $(TOOL),它显示了 A。我做错了什么?

谢谢!!

编辑:添加 Makefile 示例。

对于我的问题,将包含移动到之后导出解决了我的问题。但这是一个现有的 Makefile,并且在最顶部包含包含始终有效!

include 语句在本地的顶部,所有的导出在底部。只有我的新出口线失败。有谁知道为什么?

本地 Makefile

export TOOL A
include /path/to/master/Makefile
export VERSION IMP-IR5

主 Makefile

export VERSION IAP-100

ci:
ifeq ( $(TOOL), A )
    echo "Tool A will be run..."
    [syntax for toolA]
    echo $(VERSION)
else
    echo "Tool B will be run..."
    [syntax for toolB]
    echo $(VERSION)
endif

输出

echo "Tool A will be run..."
Tool A will be run...
echo IMP-IR5
IMP-IR5

但如果我将顶部切换到本地 Makefile 中的行(就像原来一样):

include /path/to/master/Makefile
export TOOL A
export VERSION IMP-IR5

我明白了:

echo "Tool B will be run..."
Tool B will be run...
echo IMP-IR5
IMP-IR5

为什么 IMP-IR5 可以通过而不是工具 A?好迷茫……

【问题讨论】:

    标签: unix if-statement makefile global-variables


    【解决方案1】:

    尝试将 export TOOL = A 行移到本地 Makefile 中的 include 语句上方。还将ifeq ( $(TOOL), A ) 更改为ifeq ($(TOOL),A)

    【讨论】:

      【解决方案2】:

      将变量定义放在include语句之前:

      export TOOL = A
      include /path/to/master/Makefile/
      

      否则包含的 Makefile 中的规则将看不到它。

      另外,请注意条件中的空格:

      ifeq ($(TOOL),A)
      

      编辑:

      简单!您正在以不同的方式使用这两个变量。 Make从上到下评估makefile,在重建任何目标之前决定要重建哪些目标,然后执行规则,使用变量获得的任何值。考虑这个makefile:

      # I've added '@' to make it quieter.
      ci:
      ifeq ( $(TOOL), A )
          @echo "Tool A will be run..."
          @echo $(VERSION)
      else
          @echo "Tool B will be run..."
          @echo $(VERSION)
      endif
      
      # (Never mind the "export". You aren't calling $(MAKE),
      # you're just including a makefile.)
      TOOL = A
      VERSION = IMP-IR5
      

      Make 到达 if 行,TOOL 尚未定义,因此它的计算结果为空,因此 makefile 读取为:

      ci:
          @echo "Tool B will be run..."
          @echo $(VERSION)
      
      TOOL = A
      VERSION = IMP-IR5
      

      Mow Make 已确定它将执行ci 规则,TOOL = A(为时已晚)和VERSION = IMP-IR5。因此:

      Tool B will be run...
      IMP-IR5
      

      【讨论】:

      • 也谢谢你!但奇怪的是,这个Master和local是别人写的,他们在local的顶部有include,在底部有所有的exports,所以我跟着那个。我只是像你和@j.w.r 一样更改了我的新 TOOL 变量的顺序。说,它工作,但之前工作的所有其他出口在底部都很好,只有我的新出口失败了。你知道为什么吗? =/
      • @RaytheonLiszt:我不知道,但我很感兴趣。您能否将其缩减为显示此模式的最小 makefile,并将其附加到您的问题中?
      • 好的,添加了。我希望你能弄清楚:(
      • 神圣的cr@p!非常感谢!它是如此简单,但直到现在我才看到它解释得这么好。我希望我可以将两个答案标记为已接受!再次感谢!!
      • @RaytheonLiszt:“我可以活两个月,因为得到了很好的赞美。” ——马克吐温
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      相关资源
      最近更新 更多