Skip to content

Instantly share code, notes, and snippets.

@KennFatt
Last active August 1, 2020 18:00
Show Gist options
  • Save KennFatt/5cd375b58c4ecbfa64dd6a4aadd496f6 to your computer and use it in GitHub Desktop.
Save KennFatt/5cd375b58c4ecbfa64dd6a4aadd496f6 to your computer and use it in GitHub Desktop.
My idiomatic Makefile (for a moment)
# Project name, it's depends on current directory basename.
PROJECT_NAME = $(shell basename $(PWD))
# C++ Compiler
CC = clang++
# Compile flags
CFLAGS = -Wall -std=c++17
# Static libs
CLIBS = -lsfml-window -lsfml-graphics -lsfml-system
# Target build
# debug -> enable debug symbol (-g flag)
# release -> enable optimizations (-O2)
# Usage: make T=release | debug
BUILD_TARGET ?= $(or $(T), debug)
# Target directory (binary)
DIR_BIN_TARGET = target/${BUILD_TARGET}
# Target directory (*.o files)
DIR_OUT_TARGET = ${DIR_BIN_TARGET}/out
# Append extra CFLAGS depends on BUILD_TARGET
CFLAGS += $(if $(filter ${BUILD_TARGET},debug), -g, -O2)
all: init_target_dir main.o
${CC} ${DIR_OUT_TARGET}/* -o ${DIR_BIN_TARGET}/${PROJECT_NAME} ${CLIBS}
main.o: src/main.cc
${CC} ${CFLAGS} -c $< -o ${DIR_OUT_TARGET}/$@
# Create a target directory
init_target_dir:
@mkdir -p ${DIR_OUT_TARGET}
# Clean the target build's directory
clean:
@rm -rf ${DIR_BIN_TARGET}
@echo "[Makefile] `${DIR_BIN_TARGET}` has ben purged!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment