-
-
Save RandyMcMillan/dfdcf682bdfb2005090a8ea0b07be52e to your computer and use it in GitHub Desktop.
Generic makefile for C/C++ programs
This file contains hidden or 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
# Thomas Daley | |
# September 13, 2021 | |
# A generic build template for C/C++ programs | |
# executable name | |
EXE = app | |
# C compiler | |
CC = gcc | |
# C++ compiler | |
CXX = g++ | |
# linker | |
LD = g++ | |
# C flags | |
CFLAGS = | |
# C++ flags | |
CXXFLAGS = | |
# C/C++ flags | |
CPPFLAGS = -Wall | |
# dependency-generation flags | |
DEPFLAGS = -MMD -MP | |
# linker flags | |
LDFLAGS = | |
# library flags | |
LDLIBS = | |
# build directories | |
BIN = bin | |
OBJ = obj | |
SRC = src | |
SOURCES := $(wildcard $(SRC)/*.c $(SRC)/*.cc $(SRC)/*.cpp $(SRC)/*.cxx) | |
OBJECTS := \ | |
$(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(wildcard $(SRC)/*.c)) \ | |
$(patsubst $(SRC)/%.cc, $(OBJ)/%.o, $(wildcard $(SRC)/*.cc)) \ | |
$(patsubst $(SRC)/%.cpp, $(OBJ)/%.o, $(wildcard $(SRC)/*.cpp)) \ | |
$(patsubst $(SRC)/%.cxx, $(OBJ)/%.o, $(wildcard $(SRC)/*.cxx)) | |
# include compiler-generated dependency rules | |
DEPENDS := $(OBJECTS:.o=.d) | |
# compile C source | |
COMPILE.c = $(CC) $(DEPFLAGS) $(CFLAGS) $(CPPFLAGS) -c -o $@ | |
# compile C++ source | |
COMPILE.cxx = $(CXX) $(DEPFLAGS) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ | |
# link objects | |
LINK.o = $(LD) $(LDFLAGS) $(LDLIBS) $(OBJECTS) -o $@ | |
.DEFAULT_GOAL = all | |
.PHONY: all | |
all: $(BIN)/$(EXE) | |
$(BIN)/$(EXE): $(SRC) $(OBJ) $(BIN) $(OBJECTS) | |
$(LINK.o) | |
$(SRC): | |
mkdir -p $(SRC) | |
$(OBJ): | |
mkdir -p $(OBJ) | |
$(BIN): | |
mkdir -p $(BIN) | |
$(OBJ)/%.o: $(SRC)/%.c | |
$(COMPILE.c) $< | |
$(OBJ)/%.o: $(SRC)/%.cc | |
$(COMPILE.cxx) $< | |
$(OBJ)/%.o: $(SRC)/%.cpp | |
$(COMPILE.cxx) $< | |
$(OBJ)/%.o: $(SRC)/%.cxx | |
$(COMPILE.cxx) $< | |
# force rebuild | |
.PHONY: remake | |
remake: clean $(BIN)/$(EXE) | |
# execute the program | |
.PHONY: run | |
run: $(BIN)/$(EXE) | |
./$(BIN)/$(EXE) | |
# remove previous build and objects | |
.PHONY: clean | |
clean: | |
$(RM) $(OBJECTS) | |
$(RM) $(DEPENDS) | |
$(RM) $(BIN)/$(EXE) | |
# remove everything except source | |
.PHONY: reset | |
reset: | |
$(RM) -r $(OBJ) | |
$(RM) -r $(BIN) | |
-include $(DEPENDS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment