Answer : Here :
main: main.o
g++ -o main main.o
Your executable, main
, is not linked with the Graph.o
file, thus generating an undefined reference.
Solution :
Here is a simple Makefile that you won't have to modify every time you want to add a new .cpp file to your project :
EXE := a.out # nexecutable name
SRC := $(wildcard *.cpp) # get a list of all the .cpp in the current directory
OBJ := $(SRC:.cpp=.o) # compile every .cpp file into a .o file
CXXFLAGS = -W -Wall -std=c++11 -O2
$(EXE): $(OBJ)
$(CXX) $(OBJ) -o $(EXE) # $(CXX) contains g++ by default
If you want your Makefile to react to header files modifications too :
EXE := a.out # executable name
SRC := $(wildcard *.cpp) # get a list of all the .cpp in the current directory
OBJ := $(SRC:.cpp=.o) # compile every .cpp file into a .o file
DEP := $(OBJ:.o=.d) # from every .o file create a .d file that will keep track of header files
CXXFLAGS = -W -Wall -std=c++11 -O2
CPPFLAGS = -MMD -MP # let g++ handles the header files dependencies for you
-include $(DEP) # includes the .d files into the Makefile
$(EXE): $(OBJ)
$(CXX) $(OBJ) -o $(EXE) # $(CXX) contains g++ by default