In your Makefile, you are missing a
-c
option in the rule for songs.o.
While there's nothing wrong with your Makefile, you are duplicating much of the "magic" that make already knows how to do. You can simplify things a bit like this:
CXXFLAGS = -Wall -Wextra -ggdb
OBJS = main.o songs.o
#CPPFLAGS = -I ./include # assuming local include directory
#LDFLAGS = -L ./lib # assuming local lib
#LDLIBS = -lmylib -lpthread # add mylib and libptread to compile
output: $(OBJS)
$(CXX) -ggdb $^ $(LDFLAGS) $(LDLIBS) -o $@
songs.o: songs.cpp songs.h
clean:
rm -f $(OBJS) output
This should compile your project for you, and as you add new files you will only need to update the OBJS variable, and, of course, add the dependencies of
file.h
for
file.cpp
. The make built-in rules will add CXXFLAGS and CPPFLAGS to for the rules to build the
.o
files for you, so if you need to change the compiler flags, say to add more warnings, or turn on optimization (-O2 eg) , you just need to update the CXXFLAGS definition. Just in case you're not aware,
#
starts a comment in Make, so the CPPFLAGS, etc are not actually assigned in this example, which means you could leave out the
$(LDFLAGS) $(LDLIBS)
from the recipe to produce output. I included them here to show how you would use them if they were needed.