-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
61 lines (46 loc) · 1.46 KB
/
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
61
# Auto-dependency Makefile
# Copyright (C) 2015 Ahsen Uppal
# This program can be distributed under the terms
# of the GNU GENERAL PUBLIC LICENSE, Version 3.
# See the file LICENSE.
PROG:=$(notdir $(CURDIR))
CFLAGS+=-Wall -g -O0
# See:
# http://scottmcpeak.com/autodepend/autodepend.html
# maybe it's not such a good idea to mingle cxx and c flags
CXXFLAGS+=$(CFLAGS)
SRC:=$(wildcard *.c) $(wildcard *.cc)
# extra libs for link stage, also see $(LOADLIBES), $(LDLIBS)
LDFLAGS+=-lstdc++ -lpthread
LDLIBS+=
# include directives are pre-processor related, so they should be in CPPFLAGS
CPPFLAGS+=
CXXCPPFLAGS+=-std=c++11
BASE:=$(basename $(SRC))
OBJ:=$(addsuffix .o,$(BASE))
DEP:=$(addsuffix .d,$(BASE))
.PHONY: all
all: $(PROG)
# link stage
# it's also possible to use the auto-generated rule for $(PROG)
$(PROG): $(OBJ)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LDLIBS)
# the problem with this approach is when different source files need
# different include flags
%.o: %.c %.d
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
%.o: %.cc %.d
$(CXX) $(CPPFLAGS) $(CXXCPPFLAGS) $(CXXFLAGS) -c $< -o $@
# automatically create prerequistes, gcc will take care of headers
# which include other headers
%.d: %.c
$(CC) $(CPPFLAGS) -M $< -o $@
# the dep rule for %.cc doesn't work if added to the %.c rule
# as in "%.d: %.c %.cc"
%.d: %.cc
$(CXX) $(CPPFLAGS) $(CXXCPPFLAGS) -M $< -o $@
# adding a leading - supresses error message
-include $(DEP)
.PHONY: clean
clean:
$(RM) $(PROG) $(OBJ) $(DEP)