summary refs log tree commit diff
path: root/www/text.causal.agency/001-make.7
blob: b4805729c121a5a44b74d35776b15aba0973ff6c (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
.Dd September 17, 2018
.Dt MAKE 7
.Os "Causal Agency"
.
.Sh NAME
.Nm Using Make
.Nd writing less Makefile
.
.Sh DESCRIPTION
Let's talk about
.Xr make 1 .
I think an important thing to know about
.Xr make 1
is that you don't need to write a
.Pa Makefile
to use it.
There are default rules
for C, C++ and probably Fortran.
To build
.Pa foo
from
.Pa foo.c ,
just run:
.
.Pp
.Dl make foo
.
.Pp
The default rule for C files uses the
.Ev CFLAGS
variable,
so you can set that in the environment
to pass flags to the C compiler:
.
.Pp
.Dl CFLAGS=-Wall make foo
.
.Pp
It also uses
.Ev LDLIBS
for linking,
so you can add libraries with:
.
.Pp
.Dl LDLIBS=-lcurses make foo
.
.Pp
Obviously writing this every time
would become tedious,
so it might be time to write a
.Pa Makefile .
But it really doesn't need much:
.
.Bd -literal -offset indent
CFLAGS += -Wall -Wextra
LDLIBS = -lcurses

foo:
.Ed
.
.Pp
Assigning
.Ev CFLAGS
with
.Ql +=
preserves the system default
or anything passed in the environment.
Declaring
.Pa foo
as the first rule
makes it the default when
.Ql make
is run without a target.
Note that the rule doesn't need a definition;
the default will still be used.
.
.Pp
If
.Pa foo
is built from serveral source files,
unfortunately a rule definition is required:
.
.Bd -literal -offset indent
OBJS = foo.o bar.o baz.o

foo: $(OBJS)
	$(CC) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $@
.Ed
.
.Pp
This rule uses
.Ev LDFLAGS
for passing linker flags,
which is what the default rule does.
The
.Ql $@
variable here expands to
.Ql foo ,
so this rule can be copied easily
for other binary targets.
.
.Pp
If some sources depend on a header file,
they can be automatically rebuilt
when the header changes
by declaring a dependency rule:
.
.Pp
.Dl foo.o bar.o: foo.h
.
.Pp
Note that several files can appear
either side of the
.Ql ":" .
.
.Pp
Lastly,
it's always nice to add a
.Cm clean
target:
.
.Bd -literal -offset indent
clean:
	rm -f $(OBJS) foo
.Ed
.
.Pp
I hope this helps getting started with
.Xr make 1
without writing too much
.Pa Makefile !
.
.Sh EXAMPLES
The example
.Pa Makefile
in its entirety:
.
.Bd -literal -offset indent
CFLAGS += -Wall -Wextra
LDLIBS = -lcurses
OBJS = foo.o bar.o baz.o

foo: $(OBJS)
	$(CC) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $@

foo.o bar.o: foo.h

clean:
	rm -f $(OBJS) foo
.Ed
.
.Sh AUTHORS
.An Mt june@causal.agency
.
.Pp
This document is produced from
.Xr mdoc 7
source available from
.Lk https://git.causal.agency/src/tree/www/text.causal.agency