Skip to content

Instantly share code, notes, and snippets.

@jtortoise
Created August 13, 2022 13:15
Show Gist options
  • Select an option

  • Save jtortoise/9a6103a977ba0f0b61a144ba5eec9adb to your computer and use it in GitHub Desktop.

Select an option

Save jtortoise/9a6103a977ba0f0b61a144ba5eec9adb to your computer and use it in GitHub Desktop.
A simple Makefile template for C/C++ project
O_DIR := build
CREATE_O_DIR := $(shell mkdir -p $(O_DIR))
BIN := $(O_DIR)/main
SRCS := $(wildcard *.cpp)
temp_OBJS := $(addsuffix .o,$(basename $(SRCS)))
OBJS := $(temp_OBJS:%=$(O_DIR)/%)
temp_DEPENDS := $(addsuffix .d,$(basename $(SRCS)))
DEPENDS := $(temp_DEPENDS:%=$(O_DIR)/%)
#CPPFLAGS := -g -O3 -Wall -Wextra -Werror
CPPFLAGS := -g -O3 -Wall -Wextra
RM := rm -f
all: $(BIN)
$(BIN): $(OBJS)
$(CXX) -o $@ $^
$(OBJS):CPPFLAGS+= -c
$(OBJS):$(O_DIR)/%.o:%.cpp
$(CXX) $(CPPFLAGS) $< -o $@
-include $(DEPENDS)
$(DEPENDS):$(O_DIR)/%.d:%.cpp
@set -e; $(RM) $@; \
$(CXX) -MM $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[:]*,$(O_DIR)/\1.o $@:,g' < $@.$$$$ > $@; \
$(RM) $@.$$$$
.PHONY: clean
clean:
$(RM) -r $(O_DIR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment