当前位置:网站首页>Remove then add string from variable of Makefile

Remove then add string from variable of Makefile

2022-06-22 05:40:00 alex_ mianmian

As we know, use sed command can change string.

When we want to use it in Makefile, how to use it?

Let's write a Makefile as following:

LDFLAGS = -Txxhello.lds -Lhello
IOT_LDFLAGS = $(LDFLAGS) | sed 's/-T.*lds/-Talex.lds/g'

all:
        @echo $(LDFLAGS)
        @echo $(IOT_LDFLAGS)

now let's execute make:

Looks good, we got what we wanted.

However, when you plan to use $(IOT_LDFLAGS), you will find something different.

let's use echo instead of @echo in Makefile,here is the output:

Here we can see:

read line marks the $(LDFLAGS)

blue line marks the $(IOT_LDFLAGS)

@echo just show the result after echo executed. so $(IOT_LDFLAGS) is not '-Talex.lds -Lhello'.

The correct way is:

LDFLAGS = -Txxhello.lds -Lhello
#IOT_LDFLAGS = $(LDFLAGS) | sed 's/-T.*lds/-Talex.lds/g'
IOT_LDFLAGS = $(shell echo $(LDFLAGS) | sed 's/-T.*lds/-Talex.lds/g')
all:
        echo $(LDFLAGS)
        echo $(IOT_LDFLAGS)

use $(shell xxxxx) to execute sed and return the result to $(IOT_LDFLAGS).

here is make result:

Now the $(IOT_LDFLAGS) is '-Talex.lds -Lhello'

原网站

版权声明
本文为[alex_ mianmian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220523351516.html