Skip to content

Instantly share code, notes, and snippets.

@bobismijnnaam
Last active December 25, 2015 19:49
Show Gist options
  • Save bobismijnnaam/7030673 to your computer and use it in GitHub Desktop.
Save bobismijnnaam/7030673 to your computer and use it in GitHub Desktop.
Universal makefile. Vanilla makefile. Automatic makefile. Cost me a lot of time and almost every available makefile resource on the WWW to make it. I will try using this template for my projects until something comes along that actually works without too much trouble.
# Compiler of your choosing
CC := ...
# Collects all the cpp files
SRC := $(wildcard *.cpp)
# Linker Options
LOPS := ...
# Executable name (without .exe!)
PROG := ...
# Dir where objects are stored
OBJDIR = obj
# For each cpp file the compiler will make a separate object
OBJS = $(SRC:.cpp=.o)
# Add a directory prefix to each object file
OUT_OBJS = $(addprefix $(OBJDIR)/, $(OBJS))
# Dir where dependencies (*.d) are stored. Dependencies are files which tell what files a certain other file needs.
DEPDIR = deps
# For example, main.d might contain "main.o deps/main.d: main.cpp mtwist.h engine.hpp"
# This rule compiles the program.
bin/$(PROG): $(OUT_OBJS)
$(CC) -o $@ $^ $(LOPS)
# This is a rule for generating objects. It checks the corresponding dependency file for which dependencies to check
$(OBJDIR)/%.o: $(DEPDIR)/%.d
$(CC) -c $*.cpp -o $@
# To make sure the .d aren't deleted afterwards. You can probably do without it
.PRECIOUS: $(DEPDIR)/%.d
# To create dependencies out of .cpp files. Dependencies are only remade when the corresponding .cpp file changes # $@
$(DEPDIR)/%.d: %.cpp
$(CC) -MM $(CPPFLAGS) -MF $@ $< -MT $*.o
# This is needed to make the whole dependency thing work
-include $(SRC:.cpp=.d)
# Simple clean target. I don't know if the Linux version is correct but included it anyway
# del "$(OBJDIR)\*.o" "$(DEPDIR)\*.d" "bin\$(PROG).exe"
# not tested unix command
.PHONY: clean
clean:
del $(OBJDIR)\*.o
del $(DEPDIR)\*.d
del bin\$(PROG).exe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment