Skip to content

Instantly share code, notes, and snippets.

@mateuskrause
Last active August 2, 2022 23:12
Show Gist options
  • Save mateuskrause/7d94f9d2953164298a6a27f503d4d3f6 to your computer and use it in GitHub Desktop.
Save mateuskrause/7d94f9d2953164298a6a27f503d4d3f6 to your computer and use it in GitHub Desktop.
# This Makefile will check for every .cpp file (including sub-directories) inside SRC_DIR. Works under Linux and Windows.
# Edit variables according to your project.
TARGET := program
# OS specific variables
ifeq ($(OS),Windows_NT)
SYSTEM = win
SUFFIX = .exe
RUN = ./${TARGET}${SUFFIX}
OUT_DIR =
MKDIR = (@mkdir $(subst /,\,$(dir $@)))&
else
SYSTEM = linux
SUFFIX =
RUN = ./${TARGET}${SUFFIX}
OUT_DIR =
MKDIR = @mkdir -p $(@D)
endif
# recursive wildcard function by John Graham-Cunning
# https://blog.jgc.org/2011/07/gnu-make-recursive-wildcard-function.html
rwildcard = $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2)$(filter $(subst *,%,$2),$d))
# common variables
CC := g++
CXXFLAGS := -std=c++11 -g -Wall
LDFLAGS := # example: -Llibs/sfml-${SYSTEM}/lib -lsfml-audio -lsfml-graphics -lsfml-window -lsfml-system
BUILD := ./build
INCLUDE := # example: -I include -I libs/sfml-${SYSTEM}/include
SRC_DIR := src
SRC := $(call rwildcard, $(SRC_DIR), *.cpp)
OBJECTS := $(SRC:%.cpp=$(BUILD)/%.o)
# build targets
all: $(TARGET)
$(BUILD)/%.o: %.cpp
${MKDIR}
$(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -o $@
$(TARGET): $(OBJECTS)
${MKDIR}
$(CXX) $(CXXFLAGS) -o ${OUT_DIR}$(TARGET)${SUFFIX} $^ $(LDFLAGS)
# phony targets
.PHONY: run install
install:
run:
${RUN}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment