There always are cpp files and each of them has a main function as a standalone app each. For example, in my folder DesignPatterns I have files factory_example.cpp and command.cpp, they ready to be built into factory_example.exe and command.exe over Lunix. So that the following Makefile will helpful on the points.
.PHONY: all clean pre-build
SRCS = $(wildcard *.cpp)
BINS = $(patsubst %.cpp,%.exe,$(SRCS))
LDFLAGS = -L.
LDLIBS = -lpthread
CPPFLAGS = -D_DEBUG
CXXFLAGS = -O3 -g
all: pre-build $(BINS)
pre-build:
@echo "#-------------------------------------------------------------------------------"
@echo "# Build Design Patterns Examples"
@echo "#-------------------------------------------------------------------------------"
%.exe: %.o
@echo ""
@echo "# Build $@"
$(CXX) $(CPPFLAGS) $(LDFLAGS) $^ -o $@ $(LDLIBS)
@echo "#-------------------------------------------------------------------------------"
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $^ -o $@
tar:
@echo "#-------------------------------------------------------------------------------"
@echo "# Build Tarball"
@echo "#-------------------------------------------------------------------------------"
rm -rf DesignPatterns && mkdir DesignPatterns
cp $(SRCS) Makefile DesignPatterns
tar cvf design_patterns_$(shell date +"%Y%m%d").tar DesignPatterns
rm -rf DesignPatterns
clean:
$(RM) $(BINS) *.o