|
1 |
| -# If you use threads, add -pthread here. |
2 | 1 | COMPILERFLAGS = -g -Wall -Wextra -Wno-sign-compare
|
3 | 2 |
|
4 |
| -# Any libraries you might need linked in. |
5 | 3 | LINKLIBS = -lpthread
|
6 | 4 |
|
7 |
| -# The components of each program. When you create a src/foo.c source file, add obj/foo.o here, separated |
8 |
| -#by a space (e.g. SOMEOBJECTS = obj/foo.o obj/bar.o obj/baz.o). |
9 | 5 | SERVEROBJECTS = obj/receiver.o
|
10 | 6 | CLIENTOBJECTS = obj/sender.o
|
11 | 7 |
|
12 |
| -#Every rule listed here as .PHONY is "phony": when you say you want that rule satisfied, |
13 |
| -#Make knows not to bother checking whether the file exists, it just runs the recipes regardless. |
14 |
| -#(Usually used for rules whose targets are conceptual, rather than real files, such as 'clean'. |
15 |
| -#If you DIDNT mark clean phony, then if there is a file named 'clean' in your directory, running |
16 |
| -#`make clean` would do nothing!!!) |
17 | 8 | .PHONY: all clean
|
18 | 9 |
|
19 |
| -#The first rule in the Makefile is the default (the one chosen by plain `make`). |
20 |
| -#Since 'all' is first in this file, both `make all` and `make` do the same thing. |
21 |
| -#(`make obj server client talker listener` would also have the same effect). |
22 |
| -#all : obj server client talker listener |
23 | 10 | all : obj sender receiver
|
24 | 11 |
|
25 |
| -#$@: name of rule's target: server, client, talker, or listener, for the respective rules. |
26 |
| -#$^: the entire dependency string (after expansions); here, $(SERVEROBJECTS) |
27 |
| -#CC is a built in variable for the default C compiler; it usually defaults to "gcc". (CXX is g++). |
28 | 12 | receiver: $(SERVEROBJECTS)
|
29 | 13 | $(CC) $(COMPILERFLAGS) $^ -o $@ $(LINKLIBS)
|
30 | 14 |
|
31 |
| - |
32 |
| - |
33 |
| -#So, how does all of this work? This rule is saying |
34 |
| -# |
35 |
| -#"I am how you make the thing called client. If the thing called client is required, but doesn't |
36 |
| -#exist / is out of date, then the way to make it is to run |
37 |
| -#`$(CC) $(COMPILERFLAGS) $^ -o $@ $(LINKLIBS)`. |
38 |
| -#But, you can't start doing that until you are sure all of my dependencies ($(CLIENTOBJECTS)) are up to date." |
39 |
| -# |
40 |
| -#In this case, CLIENTOBJECTS is just obj/client.o. So, if obj/client.o doesn't exist or is out of date, |
41 |
| -#make will first look for a rule to build it. That rule is the 'obj/%.o' one, below; the % is a wildcard. |
42 | 15 | sender: $(CLIENTOBJECTS)
|
43 | 16 | $(CC) $(COMPILERFLAGS) $^ -o $@ $(LINKLIBS)
|
44 | 17 |
|
45 |
| -#RM is a built-in variable that defaults to "rm -f". |
46 | 18 | clean :
|
47 |
| -# $(RM) obj/*.o server client talker listener |
48 | 19 | $(RM) obj/*.o sender receiver
|
49 | 20 |
|
50 |
| -#$<: the first dependency in the list; here, src/%.c. (Of course, we could also have used $^). |
51 |
| -#The % sign means "match one or more characters". You specify it in the target, and when a file |
52 |
| -#dependency is checked, if its name matches this pattern, this rule is used. You can also use the % |
53 |
| -#in your list of dependencies, and it will insert whatever characters were matched for the target name. |
54 | 21 | obj/%.o: src/%.c
|
55 | 22 | $(CC) $(COMPILERFLAGS) -c -o $@ $<
|
56 | 23 | obj:
|
|
0 commit comments