You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29-18Lines changed: 29 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,11 +39,11 @@ For the purposes of this guide, we'll only dispose of a C project.
39
39
40
40
41
41
## <aname="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.
43
43
44
44
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.
45
45
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.
- 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.
70
70
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.
72
72
73
73
- 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.
@@ -151,16 +152,18 @@ You should see something like this on the terminal:
151
152
cc -c hello.c
152
153
cc main.c hello.o
153
154
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?
**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
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.
307
308
308
309
Below,isatableofsomeofthemostusefulones:
309
310
@@ -426,7 +427,7 @@ fclean: clean
426
427
427
428
428
429
### <aname="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.
430
431
You can define your own implicit rules by using pattern rules (just like we did before).
431
432
432
433
**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,
505
506
<td><code></code></td>
506
507
<td>Used flags when <code>CC</code> command is issued</td>
507
508
</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>
508
514
<tr>
509
515
<td><code>RM</code></td>
510
516
<td><code>rm -f</code></td>
@@ -538,7 +544,7 @@ $(NAME): $(OBJS)
538
544
...
539
545
```
540
546
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:
542
548
543
549
make: Nothing to be done for 'all'.
544
550
@@ -547,4 +553,9 @@ And there you have it! I hope this beginner's guide cleared a bit of your doubts
547
553
548
554
<divalign=center>
549
555
<strong><a href="#index-0">🚀 Go back to top 🚀</a></strong>
550
-
</div>
556
+
</div>
557
+
558
+
## <aname="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