zebck
zebck

Reputation: 334

Undefined reference - Why are some of the Methods in the header file not found when linking?

The Project that should be linked is made from 2 .cpp files. The includes and all of the references in the headers themselfes seem to be correct (noone from our team could find the error). Therefore I think that our Makefile has to be wrong or missing something.

Everytime I build the project I get a linking error on some of the functions contained in one of the headerfiles. Not all of them. The error looks like this but on multiple functions:

/usr/bin/ld: Utilities.cpp:(.text+0x2647): undefined reference to `simpleMatMultiplication(std::vector<std::vector<double, std::allocator >, std::allocator<std::vector<double, std::allocator > > >, std::vector<std::vector<double, std::allocator >, std::allocator<std::vector<double, std::allocator > > >)'

This is what the Makefile looks like:

CXX = g++
CXXFLAGS += -Wall -std=c++14 -pedantic 
LDFLAGS = 

INCLUDES = include

RM = rm -f

SRC_DIR = src
OBJ_DIR = obj
OUT_DIR = output

SRCS = $(SRC_DIR)/*.cpp
OBJS = $(OBJ_DIR)/*.o
OUTS = $(OUT_DIR)/*.txt

TARGET = waveguide

OBJS_GEN = $(OBJ_DIR)/Utilities.o
OBJS_TEST = $(OBJ_DIR)/waveguide.o $(OBJS_GEN)

.PHONY: all clean

all: $(TARGET)

waveguide: $(OBJS_TEST)
    $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
    $(CXX) $(CXXFLAGS) -I $(INCLUDES) -c -o $@ $<

clean:
    $(RM) $(TARGET) $(OBJS) $(OUTS)

The first two targets of the Makefile work perfectly fine. The object files are generated. The error occurs upon linking the two object files to generate the target.

Is there an Error in the Makefile above? Despite maybe some of the declared flags like LIBS not being used. What I really dont understand is how it is possible that some of the methods (of the same headerfile) are found and others marked as undefined references.

Using make -n provides

g++  -Wall -std=c++14 -pedantic  -I include -c -o obj/waveguide.o
src/waveguide.cpp 
g++  -Wall -std=c++14 -pedantic  -I include -c -o
obj/Utilities.o src/Utilities.cpp 
g++  -Wall -std=c++14 -pedantic  -o
waveguide obj/waveguide.o obj/Utilities.o

Upvotes: 1

Views: 1650

Answers (1)

TsRoe
TsRoe

Reputation: 136

Undefined reference in the linking process doesn't mean, that it can't find the declaration in the header file, it means that it cant find the definition/implementation in any of the provided object files.

Utilities.cpp doesn't seem to include an implementation of simpleMatMultiplication, or at least not with this exact signature. Your Makefile seems ok.

Upvotes: 4

Related Questions