Last active
January 25, 2017 18:04
-
-
Save alemures/bd95c3747c918597b948e54e4b1e0bcc to your computer and use it in GitHub Desktop.
Support multiple build types with GNU make
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Compiler flags | |
# | |
CC = gcc | |
CFLAGS = -Wall -Werror -Wextra | |
# | |
# Project files | |
# | |
SOURCES = file1.c file2.c file3.c file4.c | |
OBJECTS = $(SOURCES:.c=.o) | |
EXECUTABLE = exefile | |
BUILD_DIR = build | |
# | |
# Debug build settings | |
# | |
DEBUG_DIR = $(BUILD_DIR)/debug | |
DEBUG_EXECUTABLE = $(DEBUG_DIR)/$(EXECUTABLE) | |
DEBUG_OBJECTS = $(addprefix $(DEBUG_DIR)/, $(OBJECTS)) | |
DEBUG_FLAGS = -g -O0 -DDEBUG | |
# | |
# Release build settings | |
# | |
RELEASE_DIR = $(BUILD_DIR)/release | |
RELEASE_EXECUTABLE = $(RELEASE_DIR)/$(EXECUTABLE) | |
RELEASE_OBJECTS = $(addprefix $(RELEASE_DIR)/, $(OBJECTS)) | |
RELEASE_FLAGS = -O3 -DNDEBUG | |
.PHONY: all clean debug prep release remake | |
# Default build | |
all: release | |
# | |
# Debug rules | |
# | |
debug: prep $(DEBUG_EXECUTABLE) | |
$(DEBUG_EXECUTABLE): $(DEBUG_OBJECTS) | |
$(CC) $(CFLAGS) $(DEBUG_FLAGS) -o $(DEBUG_EXECUTABLE) $^ | |
$(DEBUG_DIR)/%.o: %.c | |
$(CC) -c $(CFLAGS) $(DEBUG_FLAGS) -o $@ $< | |
# | |
# Release rules | |
# | |
release: prep $(RELEASE_EXECUTABLE) | |
$(RELEASE_EXECUTABLE): $(RELEASE_OBJECTS) | |
$(CC) $(CFLAGS) $(RELEASE_FLAGS) -o $(RELEASE_EXECUTABLE) $^ | |
$(RELEASE_DIR)/%.o: %.c | |
$(CC) -c $(CFLAGS) $(RELEASE_FLAGS) -o $@ $< | |
# | |
# Other rules | |
# | |
prep: | |
@mkdir -p $(DEBUG_DIR) $(RELEASE_DIR) | |
remake: clean all | |
clean: | |
rm -f $(RELEASE_EXECUTABLE) $(RELEASE_OBJECTS) $(DEBUG_EXECUTABLE) $(DEBUG_OBJECTS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment