Skip to content

Instantly share code, notes, and snippets.

@geoorgeous
Created July 21, 2018 20:07
Show Gist options
  • Save geoorgeous/1713c27f5ea148d66baee40ac0d692d0 to your computer and use it in GitHub Desktop.
Save geoorgeous/1713c27f5ea148d66baee40ac0d692d0 to your computer and use it in GitHub Desktop.
A makefile that works with projects being developed on Windows.
# www.georgemcdonagh.co.uk
# This is the makefile that I'm currently using for an OpenGL/C++ project.
# It's not the most elegant or full-proof makefile but it works for me!
# Currently it has only been tested on Windows10 using the g++ compiler through mingw-w64
# Project directory tree should look something like:
# Project Root/
# ├── bin/
# │ ├── any_required_dlls.dll
# │ └── executable_appears_here.exe
# ├── inc/
# │ ├── more_include_dirs/ (specified via INCDIR)
# | └── include_files.hpp
# ├── lib/
# │ ├── more_lib_dirs/ (specified via LOBDIR)
# │ ├── lib0.a
# | └── lib1.lib
# ├── obj/
# │ ├── objs_appear_here.o
# │ └── dependencies_appear_here.d
# ├── src/
# │ └── source_files.cpp
# └── makefile
OUTNAME := prog_name #name of program
CC := g++ #compiler
LIBDIR := lib #lib directories (space-deliminated)
INCDIR := inc #include directories (space-deliminated)
SRCDIR := src #source code directory
OUTDIR := bin #output directory (for build target output)
BUILDDIR := obj #build directory (for object and dependency files)
SRCEXT := cpp #source file extension
DEPEXT := d #extension to give dependency files
OBJEXT := o #extension to give object files
LIB := opengl32 glew32 glfw3dll #libraries to link (space-deliminated)
###########################################
# FURTHER MODIFICATION MAY BREAK MAKEFILE #
###########################################
OUTPATH := $(OUTDIR)/$(OUTNAME)
LIBDIRP := $(foreach d, $(LIBDIR), -L$d)
INCDIRP := $(foreach d, $(INCDIR), -I$d)
SRCS := $(wildcard $(SRCDIR)/*.cpp)
OBJS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SRCS:.$(SRCEXT)=.$(OBJEXT)))
DEPS := $(OBJS:.o=.d)
LIBP := $(foreach l, $(LIB), -l$l)
ECHO := echo [MAKE $(OUTNAME)]:
# Default make rule
all: $(OUTNAME)
# Build main executable
$(OUTNAME): $(OBJS)
@$(ECHO) Linking...
@$(CC) $(LIBDIRP) -o $(OUTDIR)/$@ $^ $(LIBP)
# Build object files from source
$(BUILDDIR)/%.$(OBJEXT): $(SRCDIR)/%.$(SRCEXT)
@$(ECHO) Compiling: "$<" ...
@$(CC) $(INCDIRP) -c $< -o $@
# Generate dependency files
$(BUILDDIR)/%.$(DEPEXT): $(SRCDIR)/%.$(SRCEXT)
@$(ECHO) Generating dependency file: "$@" ...
@$(CPP) $(CFLAGS) $(INCDIRP) $< -MM -MT $(@:.$(DEPEXT)=.$(OBJEXT)) >$@
# Clean build; delete BUILD directory contents and main executable
.PHONY: clean
clean:
@$(ECHO) Deleting "$(BUILDDIR)/*.$(OBJEXT)", "$(BUILDDIR)/*.$(DEPEXT)", \
and main executable: "$(OUTPATH).exe" ...
@del /q "$(BUILDDIR)\*.$(OBJEXT)" 2>NUL
@del /q "$(BUILDDIR)\*.$(DEPEXT)" 2>NUL
@del /q "$(OUTDIR)\$(OUTNAME).exe" 2>NUL
# Clean and then re-make
rebuild:
@make clean
@make all
# Include dependency files
-include $(DEPS)
# Run the main executable
run:
if not exist $(OUTPATH).exe make all
@$(ECHO) Running ($(OUTPATH).exe) ...
echo ============ [PROGRAM BEGIN] ============
@$(OUTPATH).exe
echo ============= [PROGRAM END] =============
.PHONY: run rebuild
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment