【问题标题】:Append/Substitute string in Makefile from another makefile从另一个makefile在Makefile中追加/替换字符串
【发布时间】:2016-06-29 01:19:20
【问题描述】:

我想在我的主 Makefile 包含的一个 .mak 文件中用字符串替换这个字符串,如下所示:

/boo.mak

[...]

LOADLIBES += -lGoo
LOADLIBES += -lBaa -lCov -lDtt  // -lDtt to be replaced

[...]

/生成文件

[...]

include $(DIR)/boo.mak

[...]

-lDtt将被替换为-Wl, --do-something -lDtt -Wl --undo-something之类的东西

您建议如何执行此操作? 我试过这个:

/测试

[...]
replace:= -Wl, --do-something -lDtt -Wl --undo-something
dtt:= -lDtt
boo:= $(DIR)/boo.mak

newboo:= $(subst $(dtt),$(replace),$(boo))

include $(newboo)
[...]

但没有任何改变,-lDtt 仍然存在,并且在我运行后不会替换为我想要的文本。

编辑:我正在使用 gcc 和 linux。

【问题讨论】:

    标签: c++ linux makefile


    【解决方案1】:

    $(subst 的第三个参数是要替换的实际字符串。正如您所相信的那样,它不是找到该字符串的文件的名称。

    例子:

    $ cat Makefile
    replace:= -Wl, --do-something -lDtt -Wl --undo-something
    dtt:= -lDtt
    boo:= This string contains -lDtt which will be replaced
    
    newboo:= $(subst $(dtt),$(replace),$(boo))
    
    testme:
        echo $(newboo)
    $ make testme
    echo This string contains -Wl, --do-something -lDtt -Wl --undo-something which will be replaced
    This string contains -Wl, --do-something -lDtt -Wl --undo-something which will be replaced
    

    因此,在您的情况下,您需要在 LOADLIBES 变量上使用 $(subst 来替换其内容。

    【讨论】:

    • 所以这意味着我的 boo:= 将等于 LOADLIBES ?那是对的吗 ?因此boo:= $(LOADLIBES)?
    【解决方案2】:

    好的,感谢上面的回答。我设法了解出了什么问题以及我应该采取的下一步措施以使事情正常进行。这是我当前的 Makefile:

    [...]
    
    include $(DIR)/boo.mak
    
    replace:= -Wl, --do-something -lDtt -Wl --undo-something
    dtt:= -lDtt
    boo:= $(subst $(dtt),$(replace),$(LOADLIBES))
    
    LOADLIBES = $(boo) //replace old LOADLIBES variable with new modified lib
    
    [...]
    

    我愿意接受任何改进此功能的建议,但目前它运行良好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      相关资源
      最近更新 更多