Define compilation variables based on target for

后端 未结 3 1184
刺人心
刺人心 2020-12-03 15:17

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

相关标签:
3条回答
  • 2020-12-03 16:11

    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
    
    0 讨论(0)
  • 2020-12-03 16:14

    What about that?

    ifeq ($(MAKECMDGOALS),release)
        CFLAGS += -O3
    else
        CFLAGS += -O0 -ggdb
    endif
    
    0 讨论(0)
  • 2020-12-03 16:18

    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
    
    0 讨论(0)
提交回复
热议问题