Remove then add string from variable of Makefile

本文介绍了在Makefile中如何使用shell命令来修改变量的值,通过示例展示了如何使用`$(shell ...)`来执行`sed`命令,从而改变`LDFLAGS`变量中的字符串,并确保在Makefile的其他地方正确地引用修改后的变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值