【问题标题】:Linker errors from makefile来自 makefile 的链接器错误
【发布时间】:2018-09-07 23:18:15
【问题描述】:

我在调整我找到的 here 的 makefile 时遇到了一点问题。我所拥有的如下。当我运行它时,我收到数百个“未定义的引用”错误,主要是抱怨无法在UnitTest 中找到东西。比如第一个是

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

为什么会这样?这是否与自动生成依赖项的方式有关?

这是生成文件:

# 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)

【问题讨论】:

    标签: c++ makefile dependency-management


    【解决方案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)
    

    【讨论】:

    • 在我将LDFLAGS := "-L../bin" "-L/usr/local/lib" 更改为LDFLAGS := -no-pie "-L../bin" "-L/usr/local/lib" 后有效。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多