0

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?

1 Answer 1

2

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 $@ $<
Sign up to request clarification or add additional context in comments.

5 Comments

If I add Node.o at the end, I get a 'Node.o: file not recognized: File format not recognized'?
@AbhishekIyer Well, that's strange.
even with your version of the makefile, I am getting the same error. On a mac I am getting 'ignoring file Node.o, file was built was unsupported file format'
You should delete Node.o and let make recompile it for you. It likely was built incorrectly so it's not a standard object file, but as long as the time-last-modified is newer than the source files it won't be rebuilt.
cool. That fixed that issue, but now I am getting "Node.o: linker input file unused because linking not done."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.