Skip to content

Commit 4557ade

Browse files
committed
README: small changes.
1 parent 994ddc8 commit 4557ade

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

README.md

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ For the purposes of this guide, we'll only dispose of a C project.
3939

4040

4141
## <a name="index-2">2. An introduction to Makefiles</a>
42-
Typically, a Makefile is called to handle compilation and linkage of a project and its files. The Makefile uses the modification times of the files it uses to assert if any need to be remade or not.
42+
Typically, a Makefile is called to handle compilation and linkage of a project and its files. The Makefile uses the modification times of participating files to assert if re-compilation is needed or not.
4343

4444
For instance, when compiling a `C` project, the final executable file, would be the zipped version of every `.o` file, which was in turn created from the `.c` files.
4545

46-
Let's create a simple project to work with. You can find this files in the [code](/code) folder.
46+
Let's create a simple project to work with. You can find this files in the [code/example-1](/code/example-1/) folder.
4747

4848
├── hello.c
4949
├── main.c
@@ -66,23 +66,24 @@ target: pre-requisit-1 pre-requisit-2 pre-requisit-3 ...
6666
...
6767
```
6868

69-
- A `target` is the name of a rule. Its, usually, also the name of a file, but not always.
69+
- A `target` is the name of a rule. Usually, also the name of a file, but not always.
7070

71-
- A rule can have dependencies, some stuff to be fulfilled before execution, named `pre-requisits`. A pre-requisit **can be either a file or another rule**. In the last case, the dependency rule is executed first. If the pre-requisit doesn't match neither a file or a rule's name, the Makefile halts and prints an error.
71+
- A rule can have dependencies, some stuff to be fulfilled before execution, named `pre-requisits`. A pre-requisit **can be either a file or another rule**. In the last case, the dependency rule is executed first. If the pre-requisit doesn't match neither a file or a target's name, the Makefile halts and prints an error.
7272

7373
- Finally, after all pre-requisits are fulfilled, the rule can execute its `recipe`, a collection of `commands`. A rule can also have an empty recipe.
7474

7575
Each command should be indented with a **tab**, otherwise an error like this might show up:
7676

7777
Makefile:38: *** missing separator. Stop.
7878

79-
Here's an example of a perfectly valid rule that attempts to generate a `hello.o` file from a `hello.c` file:
79+
Here's an example of a rule that attempts to generate a `hello.o` file from a `hello.c` file:
8080

8181
```Makefile
8282
hello.o: hello.c
8383
clang -c hello.c
8484
```
8585

86+
As said before, there are some rules that don't need dependencies.
8687
The `clean` rule is used to clean temporary files. Those would be the object files in a C project:
8788

8889
```Makefile
@@ -151,16 +152,18 @@ You should see something like this on the terminal:
151152
cc -c hello.c
152153
cc main.c hello.o
153154

154-
This is great! The compilation worked out and finally we can execute our program and use our hello function! But what if one wanted to compile `N` more files? Would they need to create `N` more rules?
155+
This is great! The compilation worked out and finally we can execute our program and use our `hello` function! But what if one wanted to compile `N` more files? Would they need to create `N` more rules?
155156

156157

157158
<!-- ------------------------------------------------------------------ -->
158159

159160

160161
## <a name="index-5">5. Variables</a>
161-
Similar to programming languages, the Makefile syntax allows you to define variables.
162+
Similar to programming languages, the Makefile syntax allows you to define variables.
162163

163-
Variables allow you to focus your changes on one place, preventing error-prone implementations and repeated values across your Makefile.
164+
Variables are useful because:
165+
- allows you to focus your changes on one place;
166+
- prevents the repetition of values across the Makefile (which are much more error-prone)
164167

165168
Variables can only be strings (a single one or a list of strings). Here are some examples:
166169

@@ -172,9 +175,7 @@ FULL_NAME := $(FIRST_NAMES) $(LAST_NAMES) # Nuno Miguel Carvalho de Jesus
172175
173176
> **Note**: typically you should use the ':=' operator but '=' also works.
174177
175-
> **Note**: the naming convention for variables is uppercase, to distinguish from Makefile rules.
176-
177-
> **Note**: When more than one string is specified in a variable, it isn't considered as a string with spaces, but a list of strings.
178+
> **Note**: the naming convention for variables is uppercase, to distinguish from Makefile rules.
178179
179180
You can use variables in rules and other variables as well. To access their values, you must use:
180181
@@ -246,7 +247,7 @@ all: hello.o bye.o highfive.o
246247
cc main.c $(OBJS)
247248
```
248249

249-
**3.** In the first compilation, the `hello.o` file doesn't exist. The dependency must then be remade, which forces the Makefile to look for a rule.
250+
**3.** In the first compilation, the `hello.o` file doesn't exist. The dependency must then be remade, which forces the Makefile to look for a rule:
250251

251252
```Makefile
252253
%.o: %.c
@@ -260,14 +261,14 @@ hello.o: hello.c
260261
cc -c $< # The $< expands to the first dependency of this rule (hello.c)
261262
```
262263

263-
The same goes for other dependencies having a `.o` suffix. The `$<` is an **Automatic Variable**. We'll talk more about Automatic Variables up ahead.Specifically this one is used to handle variable dependency strings, since the value of the dependency is not always the same. Final expansion:
264+
The same goes for other dependencies having a `.o` suffix. The `$<` is an **Automatic Variable**. We'll talk more about Automatic Variables up ahead. Specifically this one is used to handle strings with variable values, since the value of the dependency is not always the same. Final expansion:
264265

265266
```Makefile
266267
hello.o: hello.c
267268
cc -c hello.c
268269
```
269270

270-
**4.** Finally, after all dependencies are generated following the same pattern as before, the `all` rule can execute its recipe. The `OBJS` variable is expanded again to its values
271+
**4.** Finally, after all dependencies are generated following the same pattern as before, the `all` rule can execute its recipe. The `OBJS` variable is expanded again to its values:
271272

272273
```Makefile
273274
all: hello.o bye.o highfive.o
@@ -303,7 +304,7 @@ re: fclean all
303304

304305

305306
## <a name="index-6">6. Automatic variables</a>
306-
Automatic variables are special variables used by the Makefile to dynamically compute values. In other words, you should use those, when a rule does not always have the same dependency or target name, like in the example above.
307+
Automatic variables are special variables used by the Makefile to dynamically compute values. In other words, you can use those, when a rule does not always have the same dependency or target name, like in the example above.
307308

308309
Below, is a table of some of the most useful ones:
309310

@@ -426,7 +427,7 @@ fclean: clean
426427

427428

428429
### <a name="index-7.2">7.2. Implicit Rules</a>
429-
Have you tried to remove the `%.o: %.c` rule and run `make`? You'll soon find, the Makefile is still working. But how? Makefile has its own default rules defined for specific cases, like the ones you'll see below.
430+
Have you tried to remove the `%.o: %.c` rule and run `make`? You'll soon find the Makefile is still working. But how? Makefile has its own default rules defined for specific cases, like the ones you'll see below.
430431
You can define your own implicit rules by using pattern rules (just like we did before).
431432

432433
**Implicit Rules**, also use **Implicit Variables**, which you can check in the next section. Here's some of the implicit rules:
@@ -505,6 +506,11 @@ As told before, implicit rules rely on variables already known by the Makefile,
505506
<td><code></code></td>
506507
<td>Used flags when <code>CC</code> command is issued</td>
507508
</tr>
509+
<tr>
510+
<td><code>CPPFLAGS</code></td>
511+
<td><code></code></td>
512+
<td>Extra flags used when <code>CC or CXX</code> commands are issued. <code>CPPFLAGS</code> and <code>CFLAGS</code> might appear the same, but this one was designed to contain include flags to help the compiler to locate missing header files. But you are free to override this as you want.</td>
513+
</tr>
508514
<tr>
509515
<td><code>RM</code></td>
510516
<td><code>rm -f</code></td>
@@ -538,7 +544,7 @@ $(NAME): $(OBJS)
538544
...
539545
```
540546
You can find the code in the [code/example-6](/code/example-6/) folder.
541-
This version does the same job as before. The main difference lies on the new dependency of `all`. The first compilation will assert the project executable is not a file, so it must be remade through the `$(NAME)` rule. In the second compilation, since the `project` file was created before, the dependency is fulfilled and the Makefile directly executes the `all` recipe. Since it's empty, you'll get this message:
547+
This version does the same job as before. The main difference lies on the new dependency of `all`. The first compilation will assert the project executable is not a file, so it must be remade through the `$(NAME)` rule. In the second run however, since the `project` file was created before, the dependency is fulfilled and the Makefile directly attempts to executes the `all` recipe. Since it's empty and no other recipes were run, you'll get this message:
542548

543549
make: Nothing to be done for 'all'.
544550

@@ -547,4 +553,9 @@ And there you have it! I hope this beginner's guide cleared a bit of your doubts
547553

548554
<div align=center>
549555
<strong><a href="#index-0">🚀 Go back to top 🚀</a></strong>
550-
</div>
556+
</div>
557+
558+
## <a name="index-8">Advanced Topics</a>
559+
> Still in development...
560+
## 📞 **Contact me**
561+
Feel free to ask me any questions through Slack (**ncarvalh**).

0 commit comments

Comments
 (0)