I am trying to create a makefile using example.h and example.cpp with 2 test files test1.cpp and test2.cpp. The code for each file is created and ready but I am clueless on how to create the makefile for this thing. This is what I have so far:
I have a directory name "example" with the following items:
product.h
product.cpp
test1.cpp
test2.cpp
I want to use the sequence of cammands: g++47 -std=c++11 -c -Wall -Wextra -I.
# example makefile
#
# Notes:
# - the indents must be tabs
# - if you need to specify a non-standard compiler, then set CXX
# (it defaults to g++)
# - if you need to pass flags to the linker, set LDFLAGS rather
# than alter the ${TARGET}: ${OBJECTS} rule
# - to see make's built-in rules use make -p
# target executable name
TARGET = example
# source files
SOURCES = product.cpp test1.cpp test2.cpp
# work out names of object files from sources
OBJECTS = $(SOURCES:.cpp=.o)
# compiler flags (do not include -c here as it's dealt with by the
# appropriate rules; CXXFLAGS gets passed as part of command
# invocation for both compilation (where -c is needed) and linking
# (where it's not.)
CXXFLAGS = -std=c++11 -Wall -Wextra -I.
# default target (to build all)
all: ${TARGET}
# clean target
clean:
rm ${OBJECTS} ${TARGET}
# rule to link object files to create target executable
# $@ is the target, here $(TARGET), and $^ is all the
# dependencies separated by spaces (duplicates removed),
# here ${OBJECTS}
${TARGET}: ${OBJECTS}
${LINK.cc} -o $@ $^
# no rule is needed here for compilation as make already
# knows how to do it
# header dependencies (in more complicated cases, you can
# look into generating these automatically.)
product.o : product.h test1.h test2.h
test1.o : product.h test1.h test2.h
test2.o : product.h test2.h