gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:58:46 24 4
gpt4 key购买 nike

当我执行 make clean 时,它会提示缺少文件。特别是它提示包含在 nstest.ccnstime.cc 中的 mapnameserver.h。我认为执行 make clean 会忽略所有其他目标,甚至是隐式目标。

我想要的是能够执行 make cleanmake vectornameserver 而不会提示 nstest.cc 和 nstime.cc 包含的 header 我没有还写。这可能吗?

下面是src目录下的文件

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

这是生成文件

#
# 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)

提前致谢。

最佳答案

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

如果你只是做一个 make clean,通常你不需要/不需要依赖文件,通常的做法是在 include 周围包裹适当的 if >:

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

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

关于c++ - 使用 makefile 中的隐式规则进行清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15046222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com