gpt4 book ai didi

c++ - 我的 linux c++ gnu makefile 变量扩展不符合我的预期

转载 作者:太空宇宙 更新时间:2023-11-04 10:04:43 25 4
gpt4 key购买 nike

我创建了以下小的 makefile 片段。 注意:我已将其作为我的问题的最小示例,因此它是一个毫无意义的 makefile。

TARGET = none
OBJ_BASE_DIR = obj

# Linux x86 c++ compiler
.PHONY: build_cpp_x86Linux
build_cpp_x86Linux: TARGET = x86Linux
build_cpp_x86Linux: build

OBJ_DIR = $(addsuffix /$(TARGET),$(OBJ_BASE_DIR))

$(info TARGET IS: $(TARGET))
$(info OBJ_DIR IS: $(OBJ_DIR))

build: $(OBJ_DIR)/test.o
@echo building, OBJ_DIR: $(OBJ_DIR)

# pattern rule
$(OBJ_DIR)/%.o:
@echo "compiling $@"

这是调用 make 的输出:

TARGET IS: none
OBJ_DIR IS: obj/none
compiling obj/none/test.o
building, OBJ_DIR: obj/x86Linux

从输出中您可以看到它正在尝试编译 obj/none/test.o,但我想要它做的是尝试编译 obj/x86Linux/test。 o。我不太确定这里发生了什么。我想我明白 makefile 在第一次传递时扩展了变量(这将导致 TARGET=none),但我认为一旦我调用了目标 build_cpp_x86Linux 它将再次重新扩展变量它将 TARGET 的值设置为 x86Linux...

我在这里做错了什么,应该怎么做?

最佳答案

您还可以使用:

TARGET?=none

然后在命令行上覆盖TARGET=x86Linux

您还可以使用 ifdef 或其他扫描 if 操作来根据这些参数或环境变量设置不同的变量。

关于c++ - 我的 linux c++ gnu makefile 变量扩展不符合我的预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53364833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com