0

I'm having a little trouble adapting a makefile I found here. What I have is below. When I run it, I get hundreds of "undefined reference to" errors, mostly complaining about the inability to find things in UnitTest. For example, the first is

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

Why is this happening? Does this have something to do with how the dependencies are being automatically generated?

Here's the 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)
Taylor
  • 1,797
  • 4
  • 26
  • 51

1 Answers1

1

All your undefined references must appear when line $(LINK.o) $^ is reached, this message is a link problem.

with g++ the link order matters see link order. I would try replacing

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

by

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

and

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

by

$(BIN): $(OBJS)
$(LINK.o) $^ $(LDLIBS)
PilouPili
  • 2,601
  • 2
  • 17
  • 31
  • that works after I change `LDFLAGS := "-L../bin" "-L/usr/local/lib"` to `LDFLAGS := -no-pie "-L../bin" "-L/usr/local/lib"`. Thank you – Taylor Sep 08 '18 at 00:38