我是makefile的新手。我从“使用 GNU make 管理项目”一书中学习了 makefile 创建和其他相关概念。makefile 现在已经准备好了,我需要确保我创建的文件是好的。这是生成文件
#Main makefile which does the build
#makedepend flags
DFLAGS =
#Compiler flags
#if mode variable is empty, setting debug build mode
ifeq ($(mode),release)
CFLAGS = -Wall
else
mode = debug
CFLAGS = -g -Wall
endif
CC = g++
PROG = fooexe
#each module will append the source files to here
SRC := main.cpp
#including the description
include bar/module.mk
include foo/module.mk
OBJ := $(patsubst %.cpp, %.o, $(filter %.cpp,$(SRC)))
.PHONY:all
all: information fooexe
information:
ifneq ($(mode),release)
ifneq ($(mode),debug)
@echo "Invalid build mode."
@echo "Please use 'make mode=release' or 'make mode=debug'"
@exit 1
endif
endif
@echo "Building on "$(mode)" mode"
@echo ".........................."
#linking the program
fooexe: $(OBJ)
$(CC) -o $(PROG) $(OBJ)
%.o:%.cpp
$(CC) $(CFLAGS) -c $< -o $@
depend:
makedepend -- $(DFLAGS) -- $(SRC)
.PHONY:clean
clean:
find . -name "*.o" | xargs rm -vf
rm -vf fooexe
问题
- 上面给出的 makefile 可以很好地与发布和调试版本一起使用。但它的格式是否正确?或者你看到其中有什么缺陷吗?
- 当使用make调用时,上面的 makefile 默认会调试构建。对于发布版本,make mode=release是必需的。这是正确的方法吗?
- 提供给 g++ 的调试和发布编译器标志是否正确?对于调试,我使用-g -Wall和发布,只是-Wall。这是正确的吗?
任何帮助都会很棒。