Makefile中的链接器错误

时间:2018-09-07 23:18:15

标签: c++ makefile dependency-management

在修改我发现的{{3}} makefile时遇到了一些麻烦。我所拥有的如下。运行该错误时,我会收到数百个“未定义的引用”错误,其中大部分是抱怨无法在UnitTest中查找内容。例如,第一个是

/home/t/pf/test/main.cpp:63: undefined reference to `UnitTest::RunAllTests()'

为什么会这样?这与如何自动生成依赖项有关吗?

这是makefile:

# output binary
BIN := main

# source files
SRCS := \
    main.cpp test_resamplers.cpp test_rv_eval.cpp test_rv_samp.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 $(DEPDIR))
$(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 clean

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

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

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

-include $(DEPS)

1 个答案:

答案 0 :(得分:1)

到达$(LINK.o)$ ^行时,必须显示所有未定义的引用,此消息是链接问题。

对于g ++,链接顺序很重要,请参见link order。我会尝试替换

# link object files to binary
LINK.o = $(LD) $(LDFLAGS) $(LDLIBS) -o $@

作者

# link object files to binary
LINK.o = $(LD) $(LDFLAGS) -o $@

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

作者

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