【问题标题】:Setting Variables within Makefile commands在 Makefile 命令中设置变量
【发布时间】:2012-07-22 16:11:36
【问题描述】:

我在使用 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 上执行导致错误。如果添加了“导出”,则该变量在该子 shell 中定义。

我想在生成文件本身中定义这个变量,以便在构建对象时使用。

有可能吗?还是应该复制“objs: cFiles”目标?

【问题讨论】:

标签: linux variables makefile gnu-make


【解决方案1】:

你需要target-specific variable values

此功能允许您根据 make 当前正在构建的目标为同一变量定义不同的值。

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 $@

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多