[英]Make: circular dependency dropped c++
I created a makefile based on the GNU Make the tutorial: https://www.gnu.org/savannah-checkouts/gnu/make/manual/html_node/index.html#SEC_Contents . 我创建了一个基于GNU的makefile制作教程: https : //www.gnu.org/savannah-checkouts/gnu/make/manual/html_node/index.html#SEC_Contents 。
The make file works fine on initial make, but if a file is changed and make is ran there is a circular dependency dropped message and it does not build the changed file. make文件在初始make上工作正常,但是如果文件被更改并且make被运行则会出现循环依赖性丢弃消息,并且它不会构建已更改的文件。 The dropped dependency is bin/main.o <-bin/main.o.
删除的依赖项是bin / main.o <-bin / main.o。 This makes sense as main.o should not depend on main.o.
这有意义,因为main.o不应该依赖于main.o. I searched several sites, including stackoverflow and found some useful answers, but I have not been able to resolve my issue.
我搜索了几个网站,包括stackoverflow,并找到了一些有用的答案,但我无法解决我的问题。 These two links were the most relevant to my issue: Makefile export .o file to a different path than .cpp and Makefile circular dependency error
这两个链接与我的问题最相关: Makefile将.o文件导出到与.cpp不同的路径和Makefile循环依赖性错误
Based on the above information I edited my makefile and at least now I can debug easier. 根据以上信息,我编辑了我的makefile,至少现在我可以更容易地调试了。 The dependency issue probably has to do with the subsequent target and prerequisite after wildcard expansion, but I am just not seeing it.
依赖性问题可能与通配符扩展后的后续目标和先决条件有关,但我只是没有看到它。
Here is my makefile: 这是我的makefile:
#For compilation
CC=g++
CC_FLAGS= -g -Wall -Werror -ansi -pedantic
#make[1]: main.o all <- all
#Folder structure
BIN_DIR:=bin
SRC_DIR:=src
H_DIR:=src/header
all:rshell
#VPATH = $(SRC_DIR)
H_FILES:=$(wildcard $(H_DIR)/*.h)
DEPS:$(wildcard $(BIN_DIR)/*.d)
CPP_FILES:=$(wildcard $(SRC_DIR)/*.cpp)
OBJECTS := $(CPP_FILES:$(SRC_DIR)/%.cpp=$(BIN_DIR)/%.o)
$(BIN_DIR)/%.o: $(SRC_DIR)/%.cpp $(H_DIR)/%.h
mkdir -p $(BIN_DIR)
$(CC) -I$(H_DIR) -o $@ -c $< $(CC_FLAGS)
$(BIN_DIR)/main.o:$(SRC_DIR)/main.cpp $(OBJECTS) $(H_FILES)
$(CC) -I$(H_DIR) -o $@ -c $(SRC_DIR)/main.cpp $(CC_FLAGS)
rshell: $(OBJECTS)
$(CC) -o $(BIN_DIR)/$@ $(OBJECTS) $(CC_FLAGS)
include $(DEPS)
$(MAKEFILE_LIST): ;
%:: %,v
%:: RCS/%,v
%:: RCS/%
%:: s.%
%:: SCCS/s.%
.PHONY: clean
clean:
rm -rf $(BIN_DIR)
Question1: If the wildcard expansion is causing the circular dependency, how could I fix? 问题1:如果通配符扩展导致循环依赖,我该如何解决?
Question2: How does one trace wildcard expansion? 问题2:如何跟踪通配符扩展? Would it be correct to echo the names of the files and trace that way?
回复文件的名称并以这种方式跟踪是否正确? Running make -d was useful, but I am not seeing how to avoid it.
运行make -d很有用,但我没有看到如何避免它。 My assumption is these lines are causing the circular dependencies:
我的假设是这些行导致循环依赖:
$(BIN_DIR)/main.o:$(SRC_DIR)/main.cpp $(OBJECTS) $(H_FILES)
$(CC) -I$(H_DIR) -o $@ -c $(SRC_DIR)/main.cpp $(CC_FLAGS)
Thank you for your help and insight. 感谢您的帮助和见解。
$(BIN_DIR)/main.o:$(SRC_DIR)/main.cpp $(OBJECTS) $(H_FILES)
On this line you say to build main.o , I need main.cpp *.h and *.o 在这一行你说要构建main.o,我需要main.cpp * .h和* .o
But *.o has main.o so you write main.o depend of main.o. 但* .o有main.o所以你写main.o依赖main.o. Just remove $(OBJECTS) you don't need object to build a object.
只需删除$(OBJECTS)您不需要对象来构建对象。
$(BIN_DIR)/main.o:$(SRC_DIR)/main.cpp $(H_FILES)
Don't use wildcard, it's better to explicit your sources files. 不要使用通配符,最好明确你的源文件。 Don't use CC for c++ compiler, the standard use CXX.
不要将CC用于c ++编译器,标准使用CXX。
exemple of Makefile: Makefile的例子:
NAME = foo
CXX ?= g++
RM = rm
DEBUG ?= no
LEVEL ?= 3
LIB = -l m
INCLUDE = -I include
CXXFLAGS += -Wall -Wextra -O$(LEVEL)
CXXFLAGS += -ansi -pedantic -std=c++11
CXXFLAGS += $(INCLUDE)
ifeq ($(CXX), clang++)
CXXFLAGS += -Weverything
endif
ifneq ($(DEBUG), no)
CXXFLAGS += -g
endif
LDFLAGS = $(LIB)
ifeq ($(DEBUG), no)
LDFLAGS += -s
endif
SRC = main.cpp
SRC += foo.cpp
DPD = $(SRC:.cpp=.dpd)
OBJ = $(SRC:.cpp=.o)
all : $(NAME)
$(NAME) : $(OBJ)
$(CXX) $(OBJ) -o $(NAME) $(LDFLAGS)
clean :
$(RM) -f $(OBJ)
$(RM) -f $(DPD)
fclean : clean
$(RM) -f $(NAME)
re : fclean
$(MAKE) -C .
%.dpd : %.cpp
$(CXX) -MM $(<) -o $(@) $(CXXFLAGS) -MT $(<:.cpp=.o)
%.o : %.cpp
$(CXX) -c $(<) -o $(@) $(CXXFLAGS)
.PHONY : all clean fclean re %.dpd %.o
.SUFFIXES : .o.cpp .dpd.cpp
include $(DPD)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.