makefile错误:打开依赖文件.d / file_name.Td:没有此类文件或目录

时间:2018-09-05 19:21:03

标签: gcc makefile g++

我正在尝试修改一个自动生成依赖关系的makefile(that I found here)。但是,当我运行make时,出现以下错误消息:

t@t-XPS-13-9365:~/pf/test$ make
g++ -MT .o/test_resamplers.o -MD -MP -MF .d/test_resamplers.Td -std=c++11 -g -Wall -Wextra -pedantic -I/usr/local/include/UnitTest++ -I/usr/include/eigen3 -I../include -c -o .o/test_resamplers.o test_resamplers.cpp
test_resamplers.cpp:155:1: fatal error: opening dependency file .d/test_resamplers.Td: No such file or directory
 }
 ^
compilation terminated.
Makefile:66: recipe for target '.o/test_resamplers.o' failed
make: *** [.o/test_resamplers.o] Error 1

也许我在makefile中使用了错误的DEPFLAGS变量。我确实将示例中的编译器从clang++切换到g++。这是我的makefile:

# output binary
BIN := run_tests

# source files
SRCS := \
    main.cpp test_cf_filters.cpp test_resamplers.cpp test_rv_eval.cpp \
    test_rv_samp.cpp test_utils.cpp

# intermediate directory for generated object files
OBJDIR := .o
# intermediate directory for generated dependency files
DEPDIR := .d

# object files, auto generated from source files
OBJS := $(patsubst %,$(OBJDIR)/%.o,$(basename $(SRCS)))

# compilers (at least gcc and clang) don't create the subdirectories automatically
$(shell mkdir -p $(dir $(OBJS)) >/dev/null)

# C++ compiler
CXX := g++
# linker
LD := g++

# C++ flags
CXXFLAGS := -std=c++11
# C/C++ flags
CPPFLAGS := -g -Wall -Wextra -pedantic -I/usr/local/include/UnitTest++ -I/usr/include/eigen3 -I../include
# linker flags
LDFLAGS := "-L../bin" "-L/usr/local/lib"
# flags required for dependency generation; passed to compilers
DEPFLAGS = -MT $@ -MD -MP -MF $(DEPDIR)/$*.Td
# libraries
LDLIBS := -lpf -lUnitTest++

# compile C++ source files
COMPILE.cc = $(CXX) $(DEPFLAGS) $(CXXFLAGS) $(CPPFLAGS) -c -o $@
# link object files to binary
LINK.o = $(LD) $(LDFLAGS) $(LDLIBS) -o $@
# precompile step
PRECOMPILE =
# postcompile step
POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d

all: $(BIN)

.PHONY: clean
clean:
    $(RM) -r $(OBJDIR) $(DEPDIR)

.PHONY: help
help:
    @echo available targets: all dist clean distclean install uninstall check

$(BIN): $(OBJS)
    $(LINK.o) $^

$(OBJDIR)/%.o: %.c
$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d
    $(PRECOMPILE)
    $(COMPILE.c) $<
    $(POSTCOMPILE)

$(OBJDIR)/%.o: %.cpp
$(OBJDIR)/%.o: %.cpp $(DEPDIR)/%.d
    $(PRECOMPILE)
    $(COMPILE.cc) $<
    $(POSTCOMPILE)

$(OBJDIR)/%.o: %.cc
$(OBJDIR)/%.o: %.cc $(DEPDIR)/%.d
    $(PRECOMPILE)
    $(COMPILE.cc) $<
    $(POSTCOMPILE)

$(OBJDIR)/%.o: %.cxx
$(OBJDIR)/%.o: %.cxx $(DEPDIR)/%.d
    $(PRECOMPILE)
    $(COMPILE.cc) $<
    $(POSTCOMPILE)

.PRECIOUS = $(DEPDIR)/%.d
$(DEPDIR)/%.d: ;

-include $(DEPS)

1 个答案:

答案 0 :(得分:1)

使用以下行创建对象目录:

$(shell mkdir -p $(dir $(OBJS)) >/dev/null)

但是您不会在任何地方创建DEPDIR,因此,当编译器尝试在其中创建文件时,它将失败。

您可以添加以下行以创建依赖项目录:

$(shell mkdir -p $(DEPDIR))

或者,将其添加到以前的shell函数中。