Skip to content

Instantly share code, notes, and snippets.

@skulumani
Created February 26, 2018 17:30
Show Gist options
  • Save skulumani/74f10588459973fb7178883f131b2dec to your computer and use it in GitHub Desktop.
Save skulumani/74f10588459973fb7178883f131b2dec to your computer and use it in GitHub Desktop.
Generic Makefile for a single executable
PRODUCT := ex6-10
# directories
BINDIR := bin
INCDIR := include
SRCDIR := src
BUILDDIR := build
# compiler flags
CXX := g++
LINKER := g++
INCDIRS := -I$(INCDIR)
CXXFLAGS := -std=c++11 -Wall -Wextra -pedantic
SRCFILES := $(wildcard $(SRCDIR)/*.cpp)
OBJFILES := $(patsubst $(SRCDIR)/%.cpp, $(BUILDDIR)/%.o, $(SRCFILES))
DEPFILES := $(patsubst $(SRCDIR)/%.cpp, $(BUILDDIR)/%.d, $(SRCFILES))
# link the object files
$(BINDIR)/$(PRODUCT): $(OBJFILES)
$(LINKER) $(CXXFLAGS) $^ -o $@
# compile src to object files
$(BUILDDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) $(INCDIRS) -c $< -o $@
# build dependencies
$(BUILDDIR)/%.d: $(SRCDIR)/%.cpp
$(CXX) $(INCDIRS) -MM $< | sed 's/^/$@ /' > $@
$(OBJDIR)/%.d: $(SRCDIR)/%.cpp
$(CXX) $(INCDIRS) -MM $< | sed -e 's%^%$@ %' -e 's% % $(OBJDIR)/%' > $@
-include $(DEPFILES)
@skulumani
Copy link
Author

Expects the following directory structure

project
    src - cpp
    include - hpp
    bin - executables
    build - build ouptputs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment