使用makefile中的隐式规则进行清理

时间:2013-02-23 21:57:32

标签: c++ makefile

我做make clean抱怨丢失文件。特别是它抱怨mapnameserver.hnstest.cc中包含的nstime.cc。 我认为做make clean会忽略所有其他目标,甚至隐含目标。

我想要的是能够做make cleanmake vectornameserver而不抱怨nstest.cc和nstime.cc包含我尚未编写的标题。这可能吗?

以下是src目录中的文件

nameserverinterface.h
nstest.cc
nstime.cc
vectornameserver.cc
vectornameserver.h

这是Makefile

#
# Makefile for CPP
#

# Compiler and compiler options:
CC        = /usr/local/bin/clang++
CXX       = /usr/local/bin/clang++
CXXFLAGS  = -c -pipe -O2 -Wall -W -ansi -pedantic-errors
CXXFLAGS += -Wmissing-braces -Wparentheses -Wold-style-cast
CXXFLAGS += -std=c++11 -stdlib=libc++ -nostdinc++
CXXFLAGS += -I/Users/einar/devel/libcxx/include/

LDFLAGS   = -stdlib=libc++
LDLIBS    = -L/Users/einar/devel/libcxx/lib/


SRCDIR      = ../src
LIBDIR      = ../lib
BINDIR      = ../bin
DEPDIR      = ../dep
VPATH       = $(SRCDIR):$(LIBDIR):$(BINDIR):$(DEPDIR)
LIB_INSTALL =
BIN_INSTALL =

SRC       = $(wildcard $(SRCDIR)/*.cc)
OBJ       = $(notdir $(SRC:.cc=.o))
DEP       = $(addprefix $(DEPDIR)/, $(notdir $(SRC:.cc=.d)))
PROGS     = vectornameserver

MAKEDEPEND  = $(CXX) -MM $(CPPFLAGS) -o $*.d $<
CP          = /bin/cp

###
# 
# Phony targets
#
###
.PHONY: all
all: $(PROGS)

.PHONY: folder_setup
folder_setup:
    mkdir -p $(SRCDIR)
    mkdir -p $(LIBDIR)
    mkdir -p $(BINDIR)
    mkdir -p $(DEPDIR)

.PHONY: clean
clean:
    @$(RM) $(OBJ)

.PHONY: cleaner
cleaner:
    @$(RM) $(OBJ)
    @$(RM) $(PROGS)
    @$(RM) $(DEP)
    @$(RM) $(wildcard $(DEPDIR)/*.d*)

###
#
# Set up targets for program files in this section
# a rule should look like:
# program: obj1.o obj2.o ...
#
###
vectornameserver : vectornameserver.o

###
#
# In this section automatic dependencies are handled.
#
###
$(addprefix $(DEPDIR)/, %.d): %.cc
    @set -e; rm -f $@; \
    $(CXX) -MM $(CPPFLAGS) $< > $@.$$$$; \
    sed 's,\($*\)\.o[ :]*,\1.o $@: ,g' < $@.$$$$ \
    > $@; rm -f $@.$$$$

###
#
# Include the automatically generated dependency files
#
###
include $(DEP)

提前致谢。

1 个答案:

答案 0 :(得分:1)

问题是你在makefile中有一个include指令。这隐式地使所有包含的依赖项文件隐式目标必须在主目标运行之前刷新。正是这些规则正在运行编译器并为您提供错误。

一般来说,如果你只是做一个make clean,你通常不需要/需要依赖文件,通常情况是在if周围包裹适当的include

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),cleaner)
-include $(DEP)
endif
endif

如果您执行make cleanmake cleaner,这将避免尝试包含depfiles(从而重新生成它们)。此外,include上的-前缀会在第一次运行make时抑制有关depfiles不存在的警告(如果需要,它将(重新)生成它们并重新读取makefile和depfiles。)