forked from mwilliams/lcthw-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn-c-the-hard-waych3.txt
186 lines (145 loc) · 7.34 KB
/
learn-c-the-hard-waych3.txt
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
Learn C The Hard Way A Learn Code The Hard Way Book
* Book
* Comments
* Video Courses
* Related Books
[next] [prev] [prev-tail] [tail] [up]
Chapter 3
Exercise 2: Make Is Your Python Now
In Python you ran programs by just typing python and the code you
wanted to run. The Python interpreter would just run them, and import
any other libraries and things you needed on the fly as it ran. C is a
different beast completely where you have to compile your source files
and manually stitch them together into a binary that can run on its
own. Doing this manually is a pain, and in the last exercise you just
ran make to do it.
In this exercise, you're going to get a crash course in GNU make, and
you'll be learning to use it as you learn C. Make will for the rest of
this book, be your Python. It will build your code, and run your tests,
and set things up and do all the stuff for you that Python normally
does.
The difference is, I'm going to show you smarter Makefile wizardry,
where you don't have to specify every stupid little thing about your C
program to get it to build. I won't do that in this exercise, but after
you've been using "baby make" for a while, I'll show you "master make".
3.1 Using Make
The first stage of using make is to just use it to build programs it
already knows how to build. Make has decades of knowledge on building a
wide variety of files from other files. In the last exercise you did
this already using commands like this:
__________________________________________________________________
Source 7: Building ex1 with -Wall
1$ make ex1
2# or this one too
3$ CFLAGS="-Wall" make ex1
__________________________________________________________________
In the first command you're telling make, "I want a file named ex1 to
be created." Make then does the following:
1. Does the file ex1 exist already?
2. No. Ok, is there another file that starts with ex1?
3. Yes, it's called ex1.c. Do I know how to build .c files?
4. Yes, I run this command cc ex1.c -o ex1 to build them.
5. I shall make you one ex1 by using cc to build it from ex1.c.
The second command in the listing above is a way to pass "modifiers" to
the make command. If you're not familiar with how the Unix shell works,
you can create these "environment variables" which will get picked up
by programs you run. Sometimes you do this with a command like
export CFLAGS="-Wall" depending on the shell you use. You can however
also just put them before the command you want to run, and that
environment variable will be set only while that command runs.
In this example I did CFLAGS="-Wall" make ex1 so that it would add the
command line option -Wall to the cc command that make normally runs.
That command line option tells the compiler cc to report all warnings
(which in a sick twist of fate isn't actually all the warnings
possible).
You can actually get pretty far with just that way of using make, but
let's get into making a Makefile so you can understand make a little
better. To start off, create a file with just this in it:
__________________________________________________________________
Source 8: A simple Makefile
1CFLAGS=-Wall -g
2
3clean:
4 rm -f ex1
__________________________________________________________________
Save this file as Makefile in your current directory. Make
automatically assumes there's a file called Makefile and will just run
it. Also, WARNING: Make sure you are only entering TAB characters, not
mixtures of TAB and spaces.
This Makefile is showing you some new stuff with make. First we set
CFLAGS in the file so we never have to set it again, as well as adding
the -g flag to get debugging. Then we have a section named clean which
tells make how to clean up our little project.
Make sure it's in the same directory as your ex1.c file, and then run
these commands:
__________________________________________________________________
Source 9: Running a simple Makefile
1$ make clean
2$ make ex1
__________________________________________________________________
3.2 What You Should See
If that worked then you should see this:
__________________________________________________________________
Source 10: Full build with Makefile
1$ make clean
2rm -f ex1
3$ make ex1
4cc -Wall -g ex1.c -o ex1
5ex1.c: In function 'main':
6ex1.c:3: warning: implicit declaration of function 'puts'
7$
__________________________________________________________________
Here you can see that I'm running make clean which tells make to run
our clean target. Go look at the Makefile again and you'll see that
under this I indent and then I put the shell commands I want make to
run for me. You could put as many commands as you wanted in there, so
it's a great automation tool.
__________________________________________________________________
Note 2: Did You Fix ex1.c?
If you fixed ex1.c to have #include <stdio.h> then your output will not
have the warning (which should really be an error) about puts. I have
the error here because I didn't fix it.
__________________________________________________________________
Notice also that, even though we don't mention ex1 in the Makefile,
make still knows how to build it plus use our special settings.
3.3 How To Break It
That should be enough to get you started, but first let's break this
make file in a particular way so you can see what happens. Take the
line rm -f ex1 and dedent it (move it all the way left) so you can see
what happens. Rerun make clean and you should get something like this:
__________________________________________________________________
Source 11: Bad make run
1$ make clean
2Makefile:4: *** missing separator. Stop.
__________________________________________________________________
Always remember to indent, and if you get weird errors like this then
double check you're consistently using tab characters since some make
variants are very picky.
3.4 Extra Credit
1. Create an all: ex1 target that will build ex1 with just the command
make.
2. Read man make to find out more information on how to run it.
3. Read man cc to find out more information on what the flags -Wall
and -g do.
4. Research Makefiles online and see if you can improve this one even
more.
5. Find a Makefile in another C project and try to understand what
it's doing.
[next] [prev] [prev-tail] [front] [up]
__________________________________________________________________
Please enable JavaScript to view the comments powered by Disqus.
Take An Online Video Course
You can sign up for a video course at:
http://www.udemy.com/learn-c-the-hard-way/
This course is currently being built at the same time that the book is
being built, but if you sign up now then you get early access to both
the videos and PDF of the book.
Related Books
You might want to check out these other books in the series:
1. Learn Ruby The Hard Way
2. Learn Regex The Hard Way
3. Learn SQL The Hard Way
4. Learn C The Hard Way
5. Learn Python The Hard Way
I'll be referencing other books shortly.
Copyright 2011 Zed A. Shaw. All Rights Reserved.