Skip to content

Instantly share code, notes, and snippets.

@jeremy-rifkin
Last active June 6, 2021 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremy-rifkin/4a75f7a7a6c9e20a8315a237f18d682d to your computer and use it in GitHub Desktop.
Save jeremy-rifkin/4a75f7a7a6c9e20a8315a237f18d682d to your computer and use it in GitHub Desktop.
Simple Makefile
TARGET_EXEC := your_program
BUILD_DIR := bin
SRC_DIRS := src
SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
CC := gcc
CPP := g++
WFLAGS := -Wall -Wextra -Wpedantic -Wvla
DFLAGS := -MMD -MP
CFLAGS :=
CPPFLAGS := -std=c++17
LDFLAGS :=
# two targets:
# debug (O0, asan, asserts, etc)
# release (O3, NDEBUG, etc)
TARGET = debug
# target flags
ifeq ($(TARGET), debug)
CFLAGS += -O0 -g
ifeq ($(OS),Windows_NT)
else
CFLAGS += -fsanitize=address
LDFLAGS += -fsanitize=address
endif
else ifeq ($(TARGET), release)
CFLAGS += -O3 -funroll-loops -ffast-math -flto -march=native -DNDEBUG
LDFLAGS += -flto -s
endif
CFLAGS += $(WFLAGS) $(DFLAGS)
CPPFLAGS += $(CFLAGS)
MKDIR_P ?= mkdir -p
# exe
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
$(CPP) $(OBJS) -o $@ $(LDFLAGS)
# c source
$(BUILD_DIR)/%.c.o: %.c
$(MKDIR_P) $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
# c++ source
$(BUILD_DIR)/%.cpp.o: %.cpp
$(MKDIR_P) $(dir $@)
$(CPP) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
.PHONY: clean
clean:
$(RM) -r $(BUILD_DIR)
-include $(DEPS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment