public
Last active

  • Download Gist
Makefile
Makefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.