Abhishek Iyer
Abhishek Iyer

Reputation: 614

Undefined reference error in makefile

I am having an issue with my makefile

assignment3: BSTapp.cpp BST.o
        g++ -o assignment3 BSTapp.cpp BST.o
BST.o:  BST.cpp BST.h Node.o
    g++ -c BST.cpp -o BST.o
Node.o: Node.h Node.cpp
    g++ -c Node.cpp -o Node.o

getting an undefined reference to all methods in the Node class. But if I directly compile using

g++ -o assignment3 BSTapp.cpp BST.h BST.cpp Node.h Node.cpp

everything works fine. What am I doing wrong in the makefile?

Upvotes: 0

Views: 171

Answers (1)

user529758
user529758

Reputation:

Because you left out the Node.o file from the makefile command under the assignment3: rule:

g++ -o assignment3 BSTapp.cpp BST.o

should be

g++ -o assignment3 BSTapp.cpp BST.o Node.o

Remarks:

I. Please don't compile headers themselves!

g++ -o assignment3 BSTapp.cpp BST.h BST.cpp Node.h Node.cpp

should be

g++ -o assignment3 BSTapp.cpp BST.cpp Node.cpp

II. Your Makefile is extremely unorganized. Better do this:

OBJECTS = $(patsubst %.cpp, %.o, $(wildcard *.cpp))

all: assignment3

assignment3: $(OBJECTS)
        g++ -o $@ $^

%.o: %.cpp
        g++ -c -Wall -o $@ $<

Upvotes: 2

Related Questions