在Makefile中添加/替换另一个Makefile中的字符串

Append/Substitute string in Makefile from another makefile

本文关键字:Makefile 字符串 另一个 添加 替换      更新时间:2023-10-16

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

/boo.mak

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

/Makefile

[...]
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。

$(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来替换其内容。

好的,感谢上面的答案。我设法了解出了什么问题,以及下一步我应该采取什么措施来解决问题。这是我当前的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
[...]

我愿意接受任何改进的建议,但现在它工作得很好。