/
Makefile
56 lines (42 loc) · 1.38 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
#
# Makefile from https://github.com/TheLandfill/cpp_init
#
PRODUCT := raytrace
BINDIR := ./bin
INCDIR := ./include
INCDIR_EXT := ./external_include
LIBDIR := ./libs
SRCDIR := ./src
OBJDIR := ./obj
MKDIR_P = mkdir -p
CXX := g++
LINKER := g++
INCLUDES := -I$(INCDIR) -I$(INCDIR_EXT) -I./
LIBRARIES := -L$(LIBDIR)
CXXFLAGS := -O3 -Wall -Wextra -std=c++11 -MMD -MP -ffunction-sections -fdata-sections -flto
# Finds all .cpp files and puts them into SRC
SRC := $(wildcard $(SRCDIR)/**/*.cpp)
# Creates .o files for every .cpp file in SRC (patsubst is pattern substitution)
OBJ := $(patsubst $(SRCDIR)/**/%.cpp,$(OBJDIR)/%.o,$(SRC))
# Creates .d files (dependencies) for every .cpp file in SRC
DEP := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.d,$(SRC))
# Finds all lib*.a files and puts them into LIB
LIB := $(wildcard $(LIBDIR)/lib*.a)
# $^ is list of dependencies and $@ is the target file
# Link all the object files or make a library
$(BINDIR)/$(PRODUCT): directories $(OBJ) $(LIB)
# Make a library
# ar rcs $(BINDIR)/$(PRODUCT) $(OBJ) $(LIB)
# Make a program
$(LINKER) -Wl,--gc-sections $(CXXFLAGS) $(OBJ) $(LIB) -o $@
# Compile individual .cpp source files into object files
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
-include $(DEP)
.PHONY: directories
directories: $(OBJDIR)
$(OBJDIR):
$(MKDIR_P) $(OBJDIR)
.PHONY: clean
clean:
rm -f $(OBJDIR)/* $(PRODUCT)