Skip to content

Instantly share code, notes, and snippets.

@El-Wumbus
Last active January 6, 2023 12:39
Show Gist options
  • Save El-Wumbus/7aeecd6aed7e2226626414d858c01ae4 to your computer and use it in GitHub Desktop.
Save El-Wumbus/7aeecd6aed7e2226626414d858c01ae4 to your computer and use it in GitHub Desktop.
# Important directories, relative to this Makefile
SRCDIR=src
INCDIR=include
OBJDIR=build/obj
DEPDIR=build/dep
# Main Target object/name
TARGETDIR=dist
TARGET=$(TARGETDIR)/someproject
# Compile flags
CLIBS=
CFLAGS=$(CLIBS) -I$(INCDIR) -O2 -Wall
SRCS=$(wildcard $(SRCDIR)/*.cpp) $(wildcard $(SRCDIR)/*/*.cpp)
OBJS=$(subst $(SRCDIR),$(OBJDIR),$(SRCS:.cpp=.o))
DEPS=$(subst $(SRCDIR),$(DEPDIR),$(SRCS:.cpp=.d))
# Main output executable
$(TARGET): $(OBJS)
@echo Building final target $(TARGET)...
@mkdir -p $(TARGETDIR)
@$(CC) -o $@ $^ $(CFLAGS)
@echo Done.
# Dependencies
$(DEPS): $(subst $(DEPDIR),$(SRCDIR),$(@:.d=.cpp))
@echo Generating dependency file '$@'...
@mkdir -p $(@D)
@cpp $(CFLAGS) $(subst $(DEPDIR),$(SRCDIR),$(@:.d=.cpp)) -MM -MT $(subst $(DEPDIR),$(OBJDIR),$(@:.d=.o)) > $@
@echo ' @echo Building $$@...' >> $@
@echo ' @mkdir -p $$(@D)' >> $@
@echo ' @$$(CC) -c -o $$@ $$(subst $$(OBJDIR),$$(SRCDIR),$$(@:.o=.cpp)) $$(CFLAGS)' >> $@
-include $(DEPS)
.PHONY: clean
clean:
@echo Removing $(OBJDIR), $(DEPDIR), and $(TARGET)...
@rm -fr $(OBJDIR)
@rm -fr $(DEPDIR)
@rm -fr $(TARGETDIR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment