Skip to content

Instantly share code, notes, and snippets.

@melmi

melmi/makefile

Last active Sep 23, 2016
Embed
What would you like to do?
A general purpose makefile for c++ projects
PROJNAME = your_project_name
SRCDIR = src
OBJDIR = bin
DEPDIR = bin
BINDIR = bin
CXX = g++
CXXFLAGS = -O3 -std=c++14
LDFLAGS =
TARGET = $(BINDIR)/$(PROJNAME)
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
DEPS = $(patsubst $(SRCDIR)/%.cpp,$(DEPDIR)/%.depends,$(SRCS))
.PHONY: all clean run
all: $(TARGET)
@echo Done.
$(TARGET): $(OBJS)
@echo Linking $@
@mkdir -p $(BINDIR)
@$(CXX) $(CXXFLAGS) $(OBJS) $(LDFLAGS) -o $(TARGET)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
@echo Compiling $<
@mkdir -p $(OBJDIR)
@$(CXX) $(CXXFLAGS) -c $< -o $@
$(DEPDIR)/%.depends: $(SRCDIR)/%.cpp
@echo Generating dependencies $<
@mkdir -p $(DEPDIR)
@$(CXX) -MM $(CXXFLAGS) $< -MT $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$<) -MF $@
clean:
@rm -f $(OBJS) $(DEPS) $(TARGET)
run: all
$(TARGET)
-include $(DEPS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment