Last active
September 2, 2021 09:13
-
-
Save pagdot/3bb72a961ab0120b7e6da1d16ed6ed39 to your computer and use it in GitHub Desktop.
Basic makefile
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
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 |
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
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