c++ - 使用普通make文件时如何实现cleanall

标签 c++ makefile

您好,我正在为我的项目使用通用的 makefile.inc。对于我的 src 文件夹,我定义了一个 makefile,它设置了一些变量并包含 makefile.inc。我还可以定义 DIRS= 变量(示例 #2),它将在此处指定的每个目录上调用 make -C。这一切都有效。但是,我无法让“clean”或“cleanall”正常工作。如果定义了 DIRS=,我需要一种方法来遍历列出的所有目录并调用“make -C xxx clean”。有什么想法吗?

示例 makefile #1

TYPE = exe
SOURCES = test.cpp
INCLUDES = -I. -I/usr/local/include -I../src
LIBS = -lpcre
OUT = test

include ../../makefile.inc

示例 makefile #2

DIRS = src test

include ../makefile.inc

生成文件.inc

OBJS = $(SOURCES:.cpp=.o)

ifeq ($(CFG),)
CFG=debug
endif

ifeq ($(CFG),debug)
CXXFLAGS += -g -Wall -DNDEBUG
else
CXXFLAGS += -O2 -Wall
endif

all: dirs cfgcheck $(OUT)

.PHONY: clean cleanall all

cfgcheck:
ifneq ($(CFG),release)
ifneq ($(CFG),debug)
    @echo "Error: Invalid CFG '$(CFG)'' (options: debug,release)"
    @exit 1
endif
endif
    @echo "Making '$(CURDIR)' CFG="$(CFG)

$(OUT): $(OBJS)
ifeq ($(TYPE),lib)
    $(AR) rcs $(OUT) $(OBJS)
endif
ifeq ($(TYPE),exe)
    $(CXX) -o $@ $^ ${LDFLAGS} $(LIBS)
endif

-include $(OBJS:.o=.d)

%.o: %.cpp
    $(CXX) -c $(INCLUDES) $(CXXFLAGS) $*.cpp -o $*.o
    $(CXX) -MM $(CXXFLAGS) $*.cpp > $*.d
    @cp -f $*.d $*.d.tmp
    @sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $*.d
    @rm -f $*.d.tmp

dirs: $(DIRS)
$(DIRS):
    $(MAKE) -C $@

clean:
    rm -f $(OUT) *.o *.d

最佳答案

我会做这样的事情:

DIRS = src test

clean: TARG:=clean
clean: $(DIRS)

.PHONY: $(DIRS)

$(DIRS):
        @$(MAKE) -C $@ $(TARG)

如果您不喜欢使用目录名称作为虚假目标,可以使用稍微复杂一些的替代方法...

关于c++ - 使用普通make文件时如何实现cleanall,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24592500/

相关文章:

c++ - 为什么命令 "cmake ."不生成 makefile?

c++ - Makefile: multiple definition and undefined reference 错误

assert.h 中的 C++ 断言实现

c++ - 系统调用 GetLastError() 不返回错误

c - 在 Eclipse 中处理自定义 makefile 时出现问题

python - snakemake 的 emacs 模式?

c++ - Makefile 和 .Mak 文件 + CodeBlocks 和 VStudio

c++ - 将变量与音频文件同步

c++ - 在 VS2015 中使用平台工具设置 "Microsoft CodeGen v140_clang_c2"进行编译时,包括 <functional> 和 <memory> header 会导致错误

c++ - 如何在 Carbon 中创建多个文本字段?