Skip to content

Instantly share code, notes, and snippets.

@ljmccarthy
Last active April 28, 2020 23:02
Show Gist options
  • Save ljmccarthy/4565046 to your computer and use it in GitHub Desktop.
Save ljmccarthy/4565046 to your computer and use it in GitHub Desktop.
TARGET = myprogram
SRCDIRS = src
INCDIRS = include
OBJDIR = /tmp/myprogram-build
LIBDIRS =
LIBS =
DEFINES =
PKGS =
CFLAGS = -std=c++98 -Wall -W -fvisibility=hidden -fvisibility-inlines-hidden
LINKFLAGS =
ifeq ($(strip $(DEBUG)),)
CFLAGS += -Os -fomit-frame-pointer
DEFINES += NDEBUG
else
CFLAGS += -ggdb
endif
PKG_CFLAGS = $(foreach pkg,$(PKGS),$(shell pkg-config --cflags $(pkg)))
PKG_LIBS = $(foreach pkg,$(PKGS),$(shell pkg-config --libs $(pkg)))
CFLAGS += $(PKG_CFLAGS) $(DEFINES:%=-D%) $(INCDIRS:%=-I%)
LIBFLAGS = $(PKG_LIBS) $(LIBDIRS:%=-L%) $(LIBS:%=-l%)
SRCFILES = $(foreach dir,$(SRCDIRS),$(wildcard $(dir)/*.c)) \
$(foreach dir,$(SRCDIRS),$(wildcard $(dir)/*.cxx))
DEPFILES = $(SRCFILES:%=$(OBJDIR)/%.d)
OBJFILES = $(SRCFILES:%=$(OBJDIR)/%.o)
.PHONY : all clean
all : $(TARGET)
$(TARGET) : $(OBJFILES)
@echo " EXE $@"
@g++ -pipe -o $@ $(OBJFILES) $(LINKFLAGS) $(LIBFLAGS)
ifeq ($(strip $(DEBUG)),)
@strip -s -R .comment $@
endif
clean :
@rm -rf $(TARGET) $(OBJDIR)
$(OBJDIR)/%.c.o : %.c
@echo " GCC $<"
@gcc -c -pipe -fPIC $(CFLAGS) -o $@ $<
$(OBJDIR)/%.cxx.o : %.cxx
@echo " G++ $<"
@g++ -c -pipe -fPIC $(CFLAGS) -o $@ $<
$(OBJDIR)/%.c.d : %.c
@mkdir -p $(dir $@)
@gcc -MM -MP -MT "$(OBJDIR)/$<.o" $(CFLAGS) $< > $@ || (rm -f $@; exit 1)
$(OBJDIR)/%.cxx.d : %.cxx
@mkdir -p $(dir $@)
@g++ -MM -MP -MT "$(OBJDIR)/$<.o" $(CFLAGS) $< > $@ || (rm -f $@; exit 1)
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPFILES)
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment