Skip to content

Instantly share code, notes, and snippets.

@pagdot
Last active September 2, 2021 09:13
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 pagdot/3bb72a961ab0120b7e6da1d16ed6ed39 to your computer and use it in GitHub Desktop.
Save pagdot/3bb72a961ab0120b7e6da1d16ed6ed39 to your computer and use it in GitHub Desktop.
Basic makefile
BIN_NAME=hello
# Compile and link flags
DEFINES=
#-pedantic treats warning as errors
CPPFLAGS=-Wall -Werror -pedantic -O2 -g $(addprefix -D,$(DEFINES))
LDFLAGS=
# Directory constants
BIN_DIR=bin
SRC_DIR=src
OBJ_DIR=obj
# Source and object files
BINARY=$(BIN_DIR)/$(BIN_NAME)
SOURCES=$(wildcard $(SRC_DIR)/*.cpp)
OBJECTS=$(SOURCES:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o)
#set dependency files
DEPS=$(SOURCES:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.d)
# Include all dependency files
-include $(DEPS)
# General targets
all: $(BIN_DIR) $(OBJ_DIR) $(BINARY)
clean:
rm -rf $(OBJ_DIR)
rm -rf $(BIN_DIR)
$(OBJ_DIR):
mkdir -p $(OBJ_DIR)
$(BIN_DIR):
mkdir -p $(BIN_DIR)
# Compile and link targets
$(BINARY): $(OBJECTS)
$(CXX) $^ $(LDFLAGS) -o $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) -c $< $(CPPFLAGS) -o $@ -MMD
run: all
$(BINARY)
.PHONY: all clean run
BIN_NAME=main
# Compile and link flags
DEFINES=
#-pedantic treats warning as errors
CPPFLAGS=-Wall -Werror -pedantic -O2 -g $(addprefix -D,$(DEFINES))
BINARY=main
SOURCES=main.cpp
# General targets
all: $(BINARY)
clean:
rm -rf $(OBJ_DIR)
rm -rf $(BIN_DIR)
# Compile and link targets
$(BINARY): $(SOURCES)
$(CXX) $^ $(CPPFLAGS) -o $@
run: all
./$(BINARY)
.PHONY: all clean run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment