CXXFLAGS 未附加到 makefile 中
CXXFLAGS not being appended in makefile
我有一个生成文件,我希望它能够生成发布版本和调试版本。当我只是 运行:
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
什么时候 运行?
生成文件的内容:
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
您选择的方法无效,因为
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
,否则您将无法获得调试二进制文件。
您正在做的事情的问题是您正在编辑规则的依赖项列表中的 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 ; @:
所以我们在这里查看命令目标,"parsing them"寻找您的选项并添加到标志中。
我们还需要调试和发布规则来调用构建规则(我暂时称之为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
我有一个生成文件,我希望它能够生成发布版本和调试版本。当我只是 运行:
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
什么时候 运行?
生成文件的内容:
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
您选择的方法无效,因为
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
,否则您将无法获得调试二进制文件。
您正在做的事情的问题是您正在编辑规则的依赖项列表中的 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 ; @:
所以我们在这里查看命令目标,"parsing them"寻找您的选项并添加到标志中。
我们还需要调试和发布规则来调用构建规则(我暂时称之为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