【问题标题】:CXXFLAGS not being appended in makefileCXXFLAGS 未附加在 makefile 中
【发布时间】:2019-02-08 14:39:35
【问题描述】:

我有一个生成文件,我希望它能够生成发布版本和调试版本。当我刚跑的时候:

make 

我希望 CXXFLAGS 是:

-std=c++11 -Isrc/includes -c -Os

当我跑步时

make debug

我希望 CXXFLAGS 是

-std=c++11 -Isrc/includes -c -g

我试图通过使用虚假目标并将额外的标志附加到 CXXFLAGS 变量来做到这一点,但是这些额外的标志永远不会被附加。为什么 make debug 还是会产生:

g++ -std=c++11 -Isrc/includes -c  src/file.cpp -o build/obj/file.o

不是预期的

g++ -std=c++11 -Isrc/includes -c -g src/file.cpp -o build/obj/file.o

什么时候运行?

makefile 的内容:

vpath %.cpp src/macro
vpath %.cpp src/data_types
vpath %.hpp src/includes
vpath %.cpp src

CXX := g++
CXXFLAGS := -std=c++11 -Isrc/includes -c 
LXX = g++
LXXFLAGS := 

BUILDDIR := build
OBJDIR := $(BUILDDIR)/obj

SRCS := $(notdir $(shell find -name '*.cpp'))
OBJS := $(patsubst %.cpp, $(OBJDIR)/%.o, $(SRCS))

all: release aval

aval: builddir $(OBJS) $(SRCS) 
    $(LXX) $(LXXFLAGS) $(OBJS) -o $(BUILDDIR)/aval

$(OBJDIR)/%.o: %.cpp
    $(CXX) $(CXXFLAGS) $^ -o $@

.PHONY: release
release: CXXFLAGS += -Os
release: LXXFLAGS += -s -Os

.PHONY: debug
debug:  clean db aval

.PHONY: db
db: CXXFLAGS += -g 


.PHONY: builddir
builddir:
    @mkdir -p $(OBJDIR)

.PHONY: clean
clean:
    @rm -f -r build/obj/*.o
    @rm -f build/avalanche

【问题讨论】:

标签: makefile gnu-make


【解决方案1】:

您正在做的问题是您正在编辑规则的依赖项列表中的 CXXFLAGS,由于 make 文件的解析方式,该规则不起作用。

另一种方式 - 非常简单,并且可以节省递归调用 make - 我认为这不是特别邪恶 - 但有些人会这样做。我发现这种方式肯定不那么复杂。

CXXFLAGS = defaults

ifneq (,$(findstring release,$(MAKECMDGOALS)))
  CXXFLAGS += release
else ifneq (,$(findstring debug,$(MAKECMDGOALS)))
  CXXFLAGS += debug
endif

all:
    @echo CXXFLAGS = $(CXXFLAGS)

# Rules for release / debug. The `; @:` part means the body of the rule is empty (do nothing). It just "calls" the dependency rule `all`.
release: all ; @:
debug: all ; @:

因此,我们在这里查看命令目标并“解析它们”以查找您的选项并添加到标志中。

我们还需要调试和发布规则来调用构建规则(我现在称之为all)。

这是输出:

admin@osboxes:~/sandbox/flags$ make
    CXXFLAGS = defaults
admin@osboxes:~/sandbox/flags$ make release
    CXXFLAGS = defaults release
admin@osboxes:~/sandbox/flags$ make debug
    CXXFLAGS = defaults debug

【讨论】:

    【解决方案2】:

    您选择的方法不起作用,因为

    db: CXXFLAGS += -g
    

    表示变量CXXFLAGS 已更新为包含目标db-g,但没有其他目标。 IE。这种变化并不像您预期​​的那样是全局性的。


    以下将是一种实现您想要的方式:

    .PHONY: all release
    # NOTE: must be first goal in Makefile to be default goal
    all release:
        $(MAKE) -f $(lastword $(MAKEFILE_LIST)) BUILD_CXXFLAGS="-Os" BUILD_LXXFLAGS="-s -Os" build
    
    .PHONY: debug
    debug:
        $(MAKE) -f $(lastword $(MAKEFILE_LIST)) BUILD_CXXFLAGS="-g" BUILD_LXXFLAGS="-g" build
    
    .PHONY: build
    build: clean aval
    
    CXX := g++
    CXXFLAGS := $(BUILD_CXXFLAGS) -std=c++11 -Isrc/includes -c 
    LXX = g++
    LXXFLAGS := $(BUILD_LXXFLAGS)
    
    # ... and the rest of your original Makefile ...
    

    build 实现为虚拟回显,我从上面得到以下输出:

    $ make -s
    CXX '-Os -std=c++11 -Isrc/includes -c' LXX '-s -Os'
    
    $ make -s all
    CXX '-Os -std=c++11 -Isrc/includes -c' LXX '-s -Os'
    
    $ make -s release
    CXX '-Os -std=c++11 -Isrc/includes -c' LXX '-s -Os'
    
    $ make -s debug
    CXX '-g -std=c++11 -Isrc/includes -c' LXX '-g'
    

    顺便说一句:您还需要将-g 添加到LXXFLAGS,否则您将无法获得调试二进制文件。

    【讨论】:

    • 我已将其放入,但似乎无法正常工作。在构建下我有 @echo $(CXXFLAGS) 并且当我使用 make -s debug 运行时它正在输出 -std=c++11 -Isrc/includes -c
    • 从来没有我看到这是因为 -g 设置在 LXXFLAGS 上是有意义的。
    • 对不起,我刚刚注意到我的回答中有错字。固定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    相关资源
    最近更新 更多