Created
November 24, 2020 15:51
-
-
Save danilolc/5eb6a1edac7749937f1a5ffe45049ee5 to your computer and use it in GitHub Desktop.
A small and flexible makefile example for a c++ game
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
# Danilo Lemos - 2020 | |
# A free to use and edit Makefile | |
# It compiles and links the game's | |
# source code inside src/ directory. | |
# Change the following variables as you need. | |
# Compiler: | |
CXX = g++ | |
# Optimization: | |
CXXFLAGS += -g #-O2 | |
# Warnings: | |
CXXFLAGS += -Wall | |
# Standart: | |
CXXFLAGS += --std=c++11 | |
# Libraries (SDL2 for example): | |
CXXFLAGS += $(shell pkg-config sdl2 --cflags) | |
LDFLAGS += $(shell pkg-config sdl2 --libs) -lSDL2_mixer -lSDL2_image | |
# Definitions: | |
CXXFLAGS += -DEXAMPLE_1 -DEXAMPLE_2 | |
# Directories: | |
SRC_DIR = src/ | |
BIN_DIR = bin/ | |
BUILD_DIR = build/ | |
# Source files: | |
GAME_SRC = *.cpp */*.cpp | |
GAME_SRC := $(addprefix $(SRC_DIR), $(GAME_SRC)) | |
GAME_SRC := $(wildcard $(GAME_SRC)) | |
# Object files: | |
GAME_OBJ := $(basename $(GAME_SRC)) | |
GAME_OBJ := $(subst $(SRC_DIR), ,$(GAME_OBJ)) | |
GAME_OBJ := $(addsuffix .o, $(GAME_OBJ)) | |
GAME_OBJ := $(addprefix $(BUILD_DIR), $(GAME_OBJ)) | |
# Dependency files: | |
DEPENDENCIES := $(GAME_OBJ) | |
DEPENDENCIES := $(basename $(DEPENDENCIES)) | |
DEPENDENCIES := $(addsuffix .d, $(DEPENDENCIES)) | |
# Binary output: | |
GAME_BIN = $(BIN_DIR)my-game | |
game: $(GAME_BIN) | |
########################### | |
$(GAME_BIN): $(GAME_OBJ) | |
@echo -Linking objects | |
@mkdir -p $(dir $@) >/dev/null | |
@$(CXX) $^ $(LDFLAGS) -o $@ | |
########################### | |
########################### | |
-include $(DEPENDENCIES) | |
$(BUILD_DIR)%.o: $(SRC_DIR)%.cpp | |
@echo -Compiling $< | |
@mkdir -p $(dir $@) >/dev/null | |
@$(CXX) $(CXXFLAGS) -I$(SRC_DIR) -o $@ -c $< | |
@$(CXX) -MM -MT $@ -I$(SRC_DIR) $< > $(BUILD_DIR)$*.d | |
########################### | |
clean: | |
@rm -rf $(BIN_DIR) | |
@rm -rf $(BUILD_DIR) | |
.PHONY: game clean |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment