开发者

Define compilation variables based on target for

my c++ source file look for a specific variable passed from the makefile. when making a different target, this variable definition is开发者_运维百科 different.

How can I define a variable in Makefile based on target.

Thanks


You can use target-specific variable values, they propagate to target's prerequisites:

all : foo bar
foo : CXXFLAGS += -DFOO
bar : CXXFLAGS += -DBAR

foo bar :
    @echo target=$@ CXXFLAGS=${CXXFLAGS}

.PHONY : all


Do you mean something like this:

$ cat Makefile
BUILD := debug

cxxflags.debug := -g -march=native
cxxflags.release := -g -O3 -march=native -DNDEBUG
CXXFLAGS := ${cxxflags.${BUILD}}

all :
    @echo BUILD=${BUILD}
    @echo CXXFLAGS=${CXXFLAGS}

.PHONY : all

Output:

$ make
BUILD=debug
CXXFLAGS=-g -march=native

$ make BUILD=release
BUILD=release
CXXFLAGS=-g -O3 -march=native -DNDEBUG


What about that?

ifeq ($(MAKECMDGOALS),release)
    CFLAGS += -O3
else
    CFLAGS += -O0 -ggdb
endif
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜