Skip to content

Instantly share code, notes, and snippets.

@etam
Last active May 8, 2017 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etam/3d19fcc6ce389390be77a763f60a3c96 to your computer and use it in GitHub Desktop.
Save etam/3d19fcc6ce389390be77a763f60a3c96 to your computer and use it in GitHub Desktop.
Makefile for a single binary with mixed C and C++ sources
CC := gcc
CXX := g++
CPPFLAGS :=
CFLAGS := -std=c11 -O2 -Wall -Wextra
CXXFLAGS := -std=c++14 -O2 -Wall -Wextra
LDFLAGS :=
LDLIBS := -lstdc++
COBJS :=
CXXOBJS := main.o
OBJS := $(COBJS) $(CXXOBJS)
EXE := main
all: $(EXE)
$(EXE): $(OBJS)
CDEPS := $(COBJS:.o=.d)
CXXDEPS := $(CXXOBJS:.o=.d)
DEPS := $(CDEPS) $(CXXDEPS)
$(CDEPS): %.d: %.c
@set -e; rm -f $@; \
DIR=`dirname $<`; \
$(CC) -MM $(CFLAGS) $(CPPFLAGS) $< | \
sed "s,^\(.*\)\.o:,$$DIR/\1.d $$DIR/\1.o:," > $@
$(CXXDEPS): %.d: %.cpp
@set -e; rm -f $@; \
DIR=`dirname $<`; \
$(CXX) -MM $(CXXFLAGS) $(CPPFLAGS) $< | \
sed "s,^\(.*\)\.o:,$$DIR/\1.d $$DIR/\1.o:," > $@
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif
clean:
$(RM) $(EXE) $(OBJS) $(DEPS)
.PHONY: all clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment