forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGNU_makefile_template
66 lines (54 loc) · 2.53 KB
/
GNU_makefile_template
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
62
63
64
65
66
#g++ compiler: options
# -std=c++11 enables ISO C++ 11 standard
# use -pedantic -Wall flag when compiling advised by author!
CC = g++
CCFLAGS = -pedantic -Wall -std=c++11 -I .
# Some programs include headers defined in earlier chapters
# LOCFLAGS used to set tell the compiler where to find a
# header that is not in the same directory as the source file itself
# LOCFLAGS will be set in directory level makefiles as needed
LOCFLAGS = -I ..
####### To compile without using a makefile
# To compile an object file from a source file you could execute
# g++ -pedantic -Wall -std=c++11 -I . -c filename.cpp -o filename.o
# g++ -pedantic -Wall -std=c++11 -I . -c factMain.cc -o factMain.o
# g++ -pedantic -Wall -std=c++11 -I . -c fact.cc -o fact.o
# To compile an executable file from an object file, you would execute
# g++ -pedantic -Wall -std=c++11 -I . filename.o -o filename
# g++ -pedantic -Wall -std=c++11 -I . -o factMain factMain.o fact.o
# To compile an executable file from a source file, you would execute
# g++ -pedantic -Wall -std=c++11 -I . -o factMain factMain.cpp fact.cpp
#######
# each subdirectory contains a Makefile that lists the executable
# files that can be made in that directory. That list is assigned
# to the make macro named $OBJECTS
# This rule says that the make target named "all" depends on those
# files. Executing "make all" in a subdirectory will cause make
# to build each .exe file listed in that subdirectory's makefile
all: $(OBJECTS)
# rule that says how to make a .o object file from a .cc/.cpp source file
# for a given source file in a given directory you could compile it
# into an object file by executing "make filename.o"
# $< and $@ are macros defined by make
# $< refers to the file being processed (i.e., compiled or linked)
# $@ refers to the generated file
%.o: %.cc
$(CC) $(CCFLAGS) $(LOCFLAGS) -c $< -o $@
# $< and $@ are macros defined by make
# $< refers to the file being processed (i.e., compiled or linked)
# $@ refers to the generated file
%.o: %.cpp
$(CC) $(CCFLAGS) $(LOCFLAGS) -c $< -o $@
# rule that says how to make an executable file from the object file
# for a given object file in a given directory you could compile it
# into an executable file by executing "make filename"
$(OBJECTS): % : %.o
$(CC) $(CCFLAGS) $(LOCFLAGS) $< -o $@
# target to clean up the object files
# executing "make clean" in a subdirectory will
# remove any file that is ending in .o
clean:
rm -rf *.o
# target to remove executable files as well as object files
clobber: clean
rm -rf $(OBJECTS) $(OTHOBJS)