linux - 在 Makefile 命令中设置变量

标签 linux variables makefile gnu-make

我在 GNU makefile 方面遇到了一个愚蠢的问题。 我想定义两个目标来构建一个c程序;一个有调试,另一个没有。

runNoDebug: setNoDeb objs runMe

runDebug: setDeb objs runMe

setNoDeb:
     {EXPORT} MyDEBUG= -O3

setDeb:
     {EXPORT} MyDEBUG="-DDEBUG=1 -g"

objs: cFiles
    $(CC) -o $@ $^ $(cFiles) $(CFLAGS) $(LIBS) $(MYDEBUG)

runme: objs
    ./oo

运行此 makefile 时出现错误,设置调试的命令在子 shell 上执行,导致错误。如果添加“Export”,则变量将在该子 shell 中定义。

我想在 makefile 中定义这个变量,以便在构建对象时使用。

这可能吗?或者我应该复制“objs:cFiles”目标?

最佳答案

您需要target-specific variable values :

This feature allows you to define different values for the same variable, based on the target that make is currently building.

runNoDebug: setNoDeb runMe

runDebug:   setDeb runMe

setNoDeb:   CFLAGS += -O3
setNoDeb:   objs

setDeb: CPPFLAGS += -DDEBUG=1
setDeb: CFLAGS += -g
setDeb: objs

objs: $(cFiles)
    $(CC) $(CFLAGS) $^ $(LIBS) -o $@

关于linux - 在 Makefile 命令中设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11629233/

相关文章:

c++ - 从 IDE 调用 Makefile 进行调试/发布

c - 如何构建具有相似名称的多个目标?

linux - 如果日期字符串为 + 或 - 5 分钟,则

c - 在 Linux 中禁用磁盘缓存

c - 在 C 变量定义中输入括号

c++ - "Only static constant integral variables may be initialized within a class "

awk - 如何将此命令放在Makefile中?

c++ - 堆栈变量或函数声明

python - 如何在 Python 中使用 os.umask()

c# - 使用 'var'会影响性能吗?