我有一个用于某些C ++代码的makefile(粘贴在下面),这有一个令人讨厌的问题,当我执行make clean
时,依赖文件会被编译(在再次删除之前),这也会使make clean
慢。依赖规则是
$(DEPENDDIR)%.d: %.cpp
@mkdir -p $(DEPENDDIR)
$(CXX) -M -MG -MT $(OBJECTDIR)$*.o $(CXXFLAGS) $< > $@
任何人都可以看到问题所在吗?
我尝试将依赖项移到对象被编译的部分,即:
$(OBJECTDIR)%.o: %.cpp
@mkdir -p $(OBJECTDIR) # $(dir $@)
@echo " "
$(CXX) -M -MG -MT $@ $(CXXFLAGS) $< \
-MF $(patsubst $(OBJECTDIR)%.o, $(DEPENDDIR)%.d, $@)
但是没有创建头文件version.hpp(并且编译失败)
KERNEL := $(shell uname -s)
PROGNAME=nextsim.exec
CXX = g++
# setting the C++ standard according to the gcc compiler version (from gcc-5.2, the default is C++14)
ifeq ($(shell echo `$(CXX) -dumpversion | cut -f1-2 -d.` \>= 5.2 | sed -e 's/\.//g' | bc),1)
CXXFLAGS += -std=c++14
else
CXXFLAGS += -std=c++11
endif
# add g++ option flags
CXXFLAGS += -ftemplate-depth-256 -Wno-inline \
-fPIC -fopenmp \
-DHAVE_CONFIG_H -D_MULTITHREADING_
ifdef NEXTSIM_COMPILE_VERBOSE
CXXFLAGS += -v
endif
ifdef USE_OASIS
CXXFLAGS += -DOASIS
CXXFLAGS += -I $(NEXTSIMDIR)/modules/oasis/include
LDFLAGS += -lgfortran
LDFLAGS += -L $(NEXTSIMDIR)/lib -loasis
CHAN = MPI1
#LIBPSMILE = $(OASIS_DIR)/lib/libpsmile.${CHAN}.a $(OASIS_DIR)/lib/libmct.a $(OASIS_DIR)/lib/libmpeu.a $(OASIS_DIR)/lib/libscrip.a
endif
ifneq (,$(strip $(filter DEBUG Debug debug PROFILE Profile profile,$(NEXTSIM_BUILD_TYPE))))
#ifeq ($(NEXTSIM_BUILD_TYPE),$(filter $(NEXTSIM_BUILD_TYPE),Debug debug))
CXXFLAGS := $(filter-out -O3 -pthread,$(CXXFLAGS))
CXXFLAGS += -g -O0 -DNDEBUG
ifneq (,$(strip $(filter PROFILE Profile profile,$(NEXTSIM_BUILD_TYPE))))
CXXFLAGS += -DWITHGPERFTOOLS
endif
ifneq ($(KERNEL),Linux)
CXXFLAGS += -Wl,-no_pie
endif
else
CXXFLAGS += -O3 -pthread
endif
# add include paths
CXXFLAGS += -I $(NEXTSIMDIR)/core/include
CXXFLAGS += -isystem $(NEXTSIMDIR)/contrib/bamg/include # suppress annoying compilation warnings from -I
CXXFLAGS += -isystem $(NEXTSIMDIR)/contrib/mapx/include # suppress annoying compilation warnings from -I
# CXXFLAGS += -I $(NEXTSIMDIR)/contrib/interp/include
ifdef USE_ENSEMBLE
CXXFLAGS += -DENSEMBLE
CXXFLAGS += -I $(NEXTSIMDIR)/modules/enkf/perturbation/include
endif
ifdef USE_AEROBULK
CXXFLAGS += -I $(AEROBULK_DIR)/include
CXXFLAGS += -DAEROBULK
endif
# openmpi
CXXFLAGS += -I $(OPENMPI_INCLUDE_DIR)/
# petsc
CXXFLAGS += -I $(PETSC_DIR)/include
# boost
CXXFLAGS += -I $(BOOST_INCDIR)/ -I .
# netcdf
CXXFLAGS += -I $(NETCDF_DIR)/include
# gmsh
CXXFLAGS += -I $(GMSH_DIR)/include/gmsh
CXXFLAGS += -I /opt/local/include
ifeq ($(KERNEL),Linux)
#CXXFLAGS += -std=c++0x -std=c++11 -pedantic -ftemplate-depth-256 -Wno-inline -fPIC -g -lm -pthread -v #-MMD -MP -lm -pthread -v
else
ifeq ($(CXX),clang)
CXXFLAGS += -stdlib=libc++
endif
CXXFLAGS += -I /usr/local/include #-I /opt/local/include/openmpi-mp
#LDFLAGS += -Wl,-rpath,/usr/local/lib #-Wl,-rpath,/opt/local/lib/openmpi-mp
#LDFLAGS += -L /usr/local/lib #-L /opt/local/lib/openmpi-mp -lmpi_cxx -lmpi -ldl -lstdc++ -lpthread
ifeq ($(CXX),clang)
LDFLAGS += -stdlib=libc++
endif
endif
LDFLAGS += -L /usr/local/lib
LDFLAGS += -Wl,-rpath,$(OPENMPI_LIB_DIR)/
ifndef MACHINE_HEXAGON
LDFLAGS += -L $(OPENMPI_LIB_DIR)/ -lmpi_cxx -lmpi -ldl -lstdc++ #-lpthread
else
LDFLAGS += -L $(OPENMPI_LIB_DIR)/ -lmpichcxx -lmpich -ldl -lstdc++ #-lpthread #-lssl -luuid -lpthread -lrt
LDFLAGS += -Wl,-rpath,$(BLAS_LAPACK_DIR)/lib
LDFLAGS += -L $(BLAS_LAPACK_DIR)/lib -lsci_gnu_mp
endif
LDFLAGS += -Wl,-rpath,$(NETCDF_DIR)/lib -L $(NETCDF_DIR)/lib -lnetcdf_c++4
LDFLAGS += -Wl,-rpath,$(BOOST_LIBDIR)
LDFLAGS += -L $(BOOST_LIBDIR) -lboost_program_options -lboost_filesystem -lboost_system -lboost_serialization -lboost_mpi -lboost_date_time
LDFLAGS += -Wl,-rpath,$(PETSC_DIR)/lib
LDFLAGS += -L $(PETSC_DIR)/lib -lpetsc
LDFLAGS += -Wl,-rpath,$(NEXTSIMDIR)/lib
LDFLAGS += -L $(NEXTSIMDIR)/lib -lbamg
#LDFLAGS += -L $(NEXTSIMDIR)/lib -linterp
LDFLAGS += -L $(NEXTSIMDIR)/lib -lmapx
#LDFLAGS += -L $(NEXTSIMDIR)/lib -loasis
ifdef USE_ENSEMBLE
LDFLAGS += -L $(NEXTSIMDIR)/lib -lpseudo2D
LDFLAGS += -lgfortran
endif
ifdef USE_AEROBULK
LDFLAGS += -L $(AEROBULK_DIR)/lib -laerobulk_cxx -laerobulk
LDFLAGS += -lgfortran
endif
ifneq (,$(strip $(filter DEBUG Debug debug PROFILE Profile profile,$(NEXTSIM_BUILD_TYPE))))
#ifeq ($(NEXTSIM_BUILD_TYPE),$(filter $(NEXTSIM_BUILD_TYPE),Debug debug))
LDFLAGS += -Wl,-rpath,/opt/local/lib
ifneq (,$(strip $(filter PROFILE Profile profile,$(NEXTSIM_BUILD_TYPE))))
LDFLAGS += -L /opt/local/lib -lprofiler
endif
endif
LDFLAGS += -L $(NEXTSIMDIR)/lib -lnextsim
OBJECTDIR=$(NEXTSIMDIR)/objs/
DEPENDDIR=$(NEXTSIMDIR)/.deps/
BINARYDIR=bin/
# C++ files
CXXSRCDIR=.
CXXHDRDIR=.
CXXSRC=$(wildcard $(CXXSRCDIR)/*.cpp)
# We must exclude the version.hpp file from the list of header files because otherwise we get a circular dependency
CXXHDR=$(filter-out $(CXXHDRDIR)/version.hpp, $(wildcard $(CXXHDRDIR)/*.hpp))
OBJS=$(CXXSRC:%.cpp=$(OBJECTDIR)%.o)
DEPS=$(CXXSRC:%.cpp=$(DEPENDDIR)%.d)
# Rules to always execute.
.PHONY: exec clean mrproper all cleanall mrproperall
# Default action.
exec: $(PROGNAME)
# Create a header file with the git version
version.hpp: version.sh $(CXXSRC) $(CXXHDR)
$(SHELL) -x $<
# Delete the object files.
clean:
@echo " "
$(RM) $(OBJS) $(DEPS)
@echo " "
mrproper: clean
$(RM) $(BINARYDIR)$(PROGNAME)
@echo " "
# Rule for making the actual target
lines="=========="
Lines=$(lines)$(lines)$(lines)$(lines)$(lines)$(lines)$(lines)$(lines)
$(PROGNAME): $(OBJS) #$(CCOBJS)
@mkdir -p $(BINARYDIR)
@echo " "
@echo $(Lines)$(Lines)
@echo "Creating executable: $(BINARYDIR)$(PROGNAME)"
@echo $(Lines)$(Lines)
@echo " "
$(CXX) $(CXXFLAGS) -o $(BINARYDIR)$@ $^ $(LDFLAGS)
@echo " "
@echo $(Lines)$(Lines)
@echo "Created executable: $(BINARYDIR)$(PROGNAME)"
@echo $(Lines)$(Lines)
@echo " "
# Rules for object files from cpp files
$(OBJECTDIR)%.o: %.cpp
@mkdir -p $(OBJECTDIR) # $(dir $@)
@echo " "
$(CXX) -o $@ -c $< $(CXXFLAGS)
# Make dependancy rules
$(DEPENDDIR)%.d: %.cpp
@mkdir -p $(DEPENDDIR)
$(CXX) -M -MG -MT $(OBJECTDIR)$*.o $(CXXFLAGS) $< > $@
# The compilation depends on this Makefile.
$(OBJS): Makefile
# Make everything
all:
cd ..; $(MAKE) all
# Clean everything
cleanall:
cd ..; $(MAKE) clean
# Properly clean everything
mrproperall:
cd ..; $(MAKE) mrproper
# Properly clean & recompile
fresh:
cd ..; $(MAKE) fresh
-include $(DEPS)
答案 0 :(得分:2)
由于您正在使用-include $(DEPS)
,所以正在构建它。
如果在任何这些目录中都找不到包含的makefile,则 警告消息已生成,但不是立即致命的 错误;包含包含的makefile的处理继续。 完成读取makefile文件后,make将尝试重新制作任何 过期或不存在的请参阅如何重新制作Makefile。 只有在尝试找到一种重新制作Makefile的方法并失败之后, 将诊断出丢失的Makefile为致命错误。
然后继续说:
如果您想让make仅仅忽略一个不存在的makefile或 无法重制,没有错误消息,请使用-include指令 而不是包含,例如:
-include filenames…
无论文件名(或其他任何文件名)没有错误(甚至没有警告),这一切都类似于include。 任何文件名的先决条件)不存在或不能为 重制。
诚然,这可能用不同的方式解释。我刚刚确认它确实确实尝试在-include
行上重制具有明确重制规则的文件:
all:
@echo building $@
foo.d :
@echo building $@
@touch $@
-include foo.d bar.d
哪个给:
tmp> make all
building foo.d
building all
因此,这为如何解决问题留下了一个有趣的案例。对依赖文件使用单独的规则确实会增加编译时间-您现在将每个源文件解析两次-一次生成.d一次,一次进行编译。这个不好。我认为,将依赖性和.o规则结合起来是您的最大利益。然后使依赖于version.hpp的$(OBJS)
的子集显式依赖于它,您应该会很好。
答案 1 :(得分:1)
您应该使-include $(DEPS)
行成为条件行-当MAKECMDGOALS
仅包含“干净”目标时,将其从makefile中排除:
ifneq(,$(filter-out clean distclean clobber,$(MAKECMDGOALS)))
-include $(DEPS)
endif
顺便说一句,要提防cd
成功-在其后使用&&
-或者,如果在其他目录中调用Make,则只需使用其-C
选项。