简体   繁体   English

Makefile循环依赖错误

[英]Makefile circular dependency error

I have a makefile that compiles all the cpp files in the src folder. 我有一个makefile,可以编译src文件夹中的所有cpp文件。 All of the cpp files are dependent on their .h files. 所有cpp文件都依赖于它们的.h文件。 So I have one rule to do all of that (I think). 因此,我有一条规则来做所有这些事情(我认为)。

But I want to remove the main.cpp from that list of source files, since it does not have a corresponding header file. 但是我想从该源文件列表中删除main.cpp,因为它没有相应的头文件。

I have code that removes the main from the list of cpp files. 我有从cpp文件列表中删除主要代码的代码。 Then I wrote a separate rule for compiling the main. 然后,我编写了一条单独的规则来编译主体。 I think this is where I have gone wrong. 我认为这是我犯错的地方。

Here is the makefile and the error. 这是makefile和错误。

CC := g++
CFLAGS := -g -O2
BIN_DIR := bin
BUILD_DIR := build
SRC_DIR := src
TARGET := wavfiletool.exe
MAIN := WavFileTool 

SOURCES := $(wildcard src/*.cpp)
SOURCES := $(filter-out src/$(MAIN).cpp, $(SOURCES))
OBJECTS := $(SOURCES:$(SRC_DIR)/%.cpp=$(BUILD_DIR)/%.o)

$(BIN_DIR)/$(TARGET): $(MAIN).o $(OBJECTS) 
    $(CC) $(OBJECTS) $(CFLAGS)  -o $@ 

$(MAIN).o: $(MAIN).cpp
    $(CC) $(CFLAGS) -c $(MAIN).cpp -o $(MAIN).o

$(OBJECTS): $(BUILD_DIR)/%.o : $(SRC_DIR)/%.cpp : $(SRC_DIR)/%.h
    @$(CC) $(CFLAGS) -c $< -o $@

ERROR: 错误:

make: Circular WavFileTool <- WavFileTool dependency dropped.

EDIT: When I remove the $(MAIN).o dependency from the target line, the error goes away. 编辑:当我从目标行中删除$(MAIN).o依赖项时,错误消失了。

The circular dependency is caused by a spurious space after WavFileTool , which results in your targets for $(BIN_DIR)/$(TARGET) looking like this 循环依赖关系是由WavFileTool之后的WavFileTool引起的,这导致$(BIN_DIR)/$(TARGET)看起来像这样

bin/WavFileTool.exe: WavFileTool .o (other .o files)

In the next rule WavFileTool ends up depending on itself due to the same issue: 在下一条规则中,由于相同的问题, WavFileTool最终取决于自身:

WavFileTool .o: WavFileTool .cpp

Getting rid of the space isn't enough however because you have an invalid static rule at the end (you can't "chain" targets like this), and because you haven't correctly specified the path for $(MAIN).o . 但是,摆脱空间是不够的,因为最后您有一个无效的静态规则(您不能像这样“链接”目标),并且因为您没有正确指定$(MAIN).o的路径。 。

Rather than fix your makefile, it might be more useful to use a more standard solution that generates its own dependencies instead: 与其修复您的makefile,不如使用更标准的解决方案来生成自己的依赖项,它可能更有用:

BIN_DIR   := bin
BUILD_DIR := build
SRC_DIR   := src
TARGET    := WavFileTool.exe

CC       := g++
CPPFLAGS := -MMD -MP
CXXFLAGS := -g -O2

SOURCES := $(wildcard $(SRC_DIR)/*.cpp)
OBJECTS := $(SOURCES:$(SRC_DIR)/%.cpp=$(BUILD_DIR)/%.o)
DEPS    := $(wildcard $(BUILD_DIR)/*.d)
RM       = -del $(subst /,\,$1)

.PHONY: clean

$(BIN_DIR)/$(TARGET): $(OBJECTS)
    $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@

$(OBJECTS): $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
    $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(OUTPUT_OPTION) $<

clean: ; $(call RM, $(DEPS) $(OBJECTS))

include $(DEPS)

$(MAKEFILE_LIST): ;
%:: %,v
%:: RCS/%,v
%:: RCS/%
%:: s.%
%:: SCCS/s.%

The last part after include disables remaking makefiles and some other implicit rules, which can be quite useful when debugging your make file with -d as otherwise you have to wade through a lot of unnecessary output. include之后的最后一部分禁止重新生成makefile文件和其他一些隐式规则,这在使用-d调试make文件时非常有用,否则您将不得不经过很多不必要的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM