Makefile循环依赖错误 [英] Makefile circular dependency error

查看:164
本文介绍了Makefile循环依赖错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个makefile,可以编译src文件夹中的所有cpp文件.所有cpp文件都依赖于它们的.h文件.因此,我有一条规则来完成所有这些工作(我认为).

I have a makefile that compiles all the cpp files in the src folder. All of the cpp files are dependent on their .h files. So I have one rule to do all of that (I think).

但是我想从源文件列表中删除main.cpp,因为它没有相应的头文件.

But I want to remove the main.cpp from that list of source files, since it does not have a corresponding header file.

我有一些代码可以从cpp文件列表中删除主要代码.然后,我写了一个单独的规则来编译主体.我认为这是我出问题的地方.

I have code that removes the main from the list of cpp files. Then I wrote a separate rule for compiling the main. I think this is where I have gone wrong.

这是makefile和错误.

Here is the makefile and the error.

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 $@

错误:

make: Circular WavFileTool <- WavFileTool dependency dropped.

当我从目标行中删除$(MAIN).o依赖项时,错误消失了.

When I remove the $(MAIN).o dependency from the target line, the error goes away.

推荐答案

循环依赖由WavFileTool之后的虚假空格引起,这导致$(BIN_DIR)/$(TARGET)的目标看起来像这样

The circular dependency is caused by a spurious space after WavFileTool, which results in your targets for $(BIN_DIR)/$(TARGET) looking like this

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

在下一条规则中,由于相同的问题,WavFileTool最终取决于自身:

In the next rule WavFileTool ends up depending on itself due to the same issue:

WavFileTool .o: WavFileTool .cpp

但是,摆脱空间是不够的,因为最后您有一个无效的静态规则(您不能像这样链接"目标),并且因为您没有正确指定$(MAIN).o的路径

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.

与其修复makefile,不如使用更标准的解决方案来生成自己的依赖项,这可能会更有用:

Rather than fix your makefile, it might be more useful to use a more standard solution that generates its own dependencies instead:

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.%

include之后的最后一部分禁止重新制作makefile和其他一些隐式规则,这在使用-d调试make文件时非常有用,因为否则您将需要处理很多不必要的输出.

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.

这篇关于Makefile循环依赖错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆