Skip to content

Commit 727e241

Browse files
committed
Various fixes thanks to @bgenia
- Add $(MAKEFLAGS) - Add clarification on importance of the order of flags while linking with library.
1 parent 83c2c88 commit 727e241

File tree

9 files changed

+62
-51
lines changed

9 files changed

+62
-51
lines changed

README.md

+43-35
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ used for **all the other lines**.
131131

132132
About the **equal signs**:
133133

134-
- `:=` **simply expand** the defined variable (like C equal sign).
134+
- `:=` **simply expand** the defined variable (like C `=`).
135135
- `=` **recursively expand** the defined variable (the expression is expanded
136136
afterward, when (and each time) the variable is used).
137137

@@ -163,7 +163,7 @@ all:
163163
[**Version 1 / Simplest C project**](#version-1)
164164

165165
> - 42 C coding style conventions
166-
> - `MAKE` predefined variable
166+
> - builtin variables
167167
> - The C compilation implicit rule
168168
> - pattern rule contains `%` character
169169
> - Automatic variables in practice
@@ -202,7 +202,8 @@ all:
202202
> - `addprefix` make function
203203
> - flags and libraries used by the linker
204204
> - `dir` function
205-
> - Compiling with a library recap
205+
> - Build with a library
206+
> - Linking with a library
206207
> - builds each of the required libraries
207208
> - call rules recursively
208209
@@ -231,7 +232,7 @@ The simplest, build a program called `icecream` with the following structure:
231232
### v1 Brief
232233

233234
- 42 C coding style conventions
234-
- `MAKE` predefined variable
235+
- builtin variables
235236
- The C compilation implicit rule
236237
- pattern rule contains `%` character
237238
- Automatic variables in practice
@@ -266,9 +267,10 @@ CFLAGS := -Wall -Wextra -Werror
266267
# UTENSILS #
267268
#------------------------------------------------#
268269
# RM force remove
270+
# MAKEFLAGS make flags
269271

270272
RM := rm -f
271-
MAKE := $(MAKE) --no-print-directory
273+
MAKEFLAGS += --no-print-directory
272274

273275
#------------------------------------------------#
274276
# RECIPES #
@@ -303,20 +305,23 @@ re:
303305
####################################### END_1 ####
304306
```
305307

306-
- The choice of the `CC` and `CFLAGS` values, `$(NAME)`, `clean`, `fclean`,
307-
`all` and `re` as the basic rules as well as not using a wildcard to auto
308-
generate the sources list is guided by the **42 C coding style conventions**,
309-
do not hesitate to disagree and change it (like renaming `clean` and `fclean`
310-
to the more GNU conventional `mostlyclean` and `clean` respectively).
308+
- The choice of the `CC` and `CFLAGS` values, `NAME`, `clean`, `fclean`, `all`
309+
and `re` as the basic rules as well as not using a wildcard to auto generate
310+
the sources list is guided by the **42 C coding style conventions**, do not
311+
hesitate to disagree and change it (like renaming `clean` and `fclean` to the
312+
more GNU conventional `mostlyclean` and `clean` respectively).
311313

312314
<sub><sub><hr></sub></sub>
313315

314-
- **`MAKE`** is a **predefined variable** whose value corresponds to the make
315-
executable being run, for this reason we choose to increment its options.
316-
When a Makefile is executed from another Makefile, the called's `MAKE`
317-
variable inherit from the caller's `MAKE` value. We pass it the
318-
`--no-print-directory` flag for a cleaner output, try to remove it and run
319-
`make` to see the difference.
316+
- `MAKE` and `MAKEFLAGS` are **builtin variables** like `CFLAGS` and a lot of
317+
others that you can find in the *data-base* (`make --print-data-base`
318+
command). `MAKE` value corresponds to the `make` executable being run and
319+
`MAKEFLAGS` to its flags. When a Makefile is executed from another Makefile,
320+
the called's `MAKE` `MAKEFLAGS` variables inherit from the caller's `MAKE`
321+
`MAKEFLAGS` values.
322+
323+
Here we append `--no-print-directory` to `MAKEFLAGS` content to have a clearer
324+
output, try to remove it and `make re` to see the difference.
320325

321326
<sub><sub><hr></sub></sub>
322327

@@ -331,8 +336,8 @@ Where `%.o` expands to each objects, `%.c` to each sources, `$@` to the first
331336
target (which is `%.o`) and `$<` to the leftmost prerequisite (which is `%.c`).
332337

333338
*As their name implies implicit rules are implicit and do not need to be
334-
written. All the implicit rules can be found in the data-base, accessible
335-
with a `make -p -f/dev/null | less` shell command.*
339+
written. As well as the builtin variables, all the implicit rules can be found
340+
in the data-base, accessible with `make -p -f/dev/null | less` command.*
336341

337342
<sub><sub><hr></sub></sub>
338343

@@ -485,10 +490,10 @@ CPPFLAGS := -I .
485490
# UTENSILS #
486491
#------------------------------------------------#
487492
# RM force remove
488-
# MAKE quietly make
493+
# MAKEFLAGS make flags
489494

490495
RM := rm -f
491-
MAKE := $(MAKE) --no-print-directory
496+
MAKEFLAGS += --no-print-directory
492497

493498
#------------------------------------------------#
494499
# RECIPES #
@@ -650,11 +655,11 @@ CPPFLAGS := -I include
650655
# UTENSILS #
651656
#------------------------------------------------#
652657
# RM force remove
653-
# MAKE quietly make
658+
# MAKEFLAGS make flags
654659
# DIR_DUP duplicate directory tree
655660

656661
RM := rm -f
657-
MAKE := $(MAKE) --no-print-directory
662+
MAKEFLAGS += --no-print-directory
658663
DIR_DUP = mkdir -p $(@D)
659664
```
660665

@@ -832,11 +837,11 @@ main.o: main.c main.o: main.c icecream.h
832837
# UTENSILS #
833838
#------------------------------------------------#
834839
# RM force remove
835-
# MAKE quietly make
840+
# MAKEFLAGS make flags
836841
# DIR_DUP duplicate directory tree
837842

838843
RM := rm -f
839-
MAKE := $(MAKE) --no-print-directory
844+
MAKEFLAGS += --no-print-directory
840845
DIR_DUP = mkdir -p $(@D)
841846

842847
#------------------------------------------------#
@@ -954,7 +959,8 @@ Builds an `icecream` **program that uses** `libbase` and `libarom`
954959
- `addprefix` make function
955960
- flags and libraries used by the linker
956961
- `dir` function
957-
- Compiling with a library recap
962+
- Build with a library
963+
- Linking with a library
958964
- builds each of the required libraries
959965
- call rules recursively
960966

@@ -1026,8 +1032,8 @@ LDLIBS := $(addprefix -l,$(LIBS))
10261032

10271033
<sub><sub><hr></sub></sub>
10281034

1029-
- `LDFLAGS` and `LDLIBS` contains the **flags and libraries** that will be
1030-
**used by the linker** `ld` to link the library to our project sources.
1035+
- `LDFLAGS` and `LDLIBS` contain the **flags and libraries** that will be **used
1036+
by the linker** `ld` to link the library to our project sources.
10311037

10321038
<sub><sub><hr></sub></sub>
10331039

@@ -1037,12 +1043,9 @@ LDLIBS := $(addprefix -l,$(LIBS))
10371043

10381044
<sub><sub><hr></sub></sub>
10391045

1040-
- **Compiling with a library recap**:
1041-
1042-
Compiling with a library requires, in addition to a C project and a library, a
1043-
`-I` flag that indicates to the compiler where to find the library header files,
1044-
`-L` indicate to the linker where to find the library and `-l` the name of this
1045-
library (conventionally: lib<name>).
1046+
- **Build with a library** requires three flags: `-I` tell the compiler where to
1047+
find the lib header files, `-L` tells the linker where to look for the library
1048+
and `-l` the name of this library (without its conventional `lib` prefix).
10461049

10471050
For example: `-I lib/libarom/include -L lib/libarom -l arom`
10481051

@@ -1052,11 +1055,11 @@ For example: `-I lib/libarom/include -L lib/libarom -l arom`
10521055
# UTENSILS #
10531056
#------------------------------------------------#
10541057
# RM force remove
1055-
# MAKE quietly make
1058+
# MAKEFLAGS make flags
10561059
# DIR_DUP duplicate directory tree
10571060

10581061
RM := rm -f
1059-
MAKE := $(MAKE) --silent --no-print-directory
1062+
MAKEFLAGS += --silent --no-print-directory
10601063
DIR_DUP = mkdir -p $(@D)
10611064

10621065
#------------------------------------------------#
@@ -1101,6 +1104,11 @@ re:
11011104
$(MAKE) all
11021105
```
11031106

1107+
- **Linking with a library** requires special attention to the order of the
1108+
linking flags. In our case we need to make sure that `$(LDFLAGS)` and
1109+
`$(LDLIBS)` passes respectively before and after the `$(OBJS)` in the linking
1110+
recipe.
1111+
11041112
- `$(LIBS_TARGET)` rule **builds each of the required libraries** found in the
11051113
`INGREDIENTS` part. It is a `$(NAME)` prerequisite for the same reason as
11061114
`$(OBJS)` because our *final goal* needs the libraries as well as the objects

projects/v1/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ CFLAGS := -Wall -Wextra -Werror
2222
# UTENSILS #
2323
#------------------------------------------------#
2424
# RM force remove
25-
# MAKE quietly make
25+
# MAKEFLAGS make flags
2626

2727
RM := rm -f
28-
MAKE := $(MAKE) --no-print-directory
28+
MAKEFLAGS += --no-print-directory
2929

3030
#------------------------------------------------#
3131
# RECIPES #

projects/v2/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ CPPFLAGS := -I .
2424
# UTENSILS #
2525
#------------------------------------------------#
2626
# RM force remove
27-
# MAKE quietly make
27+
# MAKEFLAGS make flags
2828

2929
RM := rm -f
30-
MAKE := $(MAKE) --no-print-directory
30+
MAKEFLAGS += --no-print-directory
3131

3232
#------------------------------------------------#
3333
# RECIPES #

projects/v3/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ CPPFLAGS := -I include
3333
# UTENSILS #
3434
#------------------------------------------------#
3535
# RM force remove
36-
# MAKE quietly make
36+
# MAKEFLAGS make flags
3737
# DIR_DUP duplicate directory tree
3838

3939
RM := rm -f
40-
MAKE := $(MAKE) --no-print-directory
40+
MAKEFLAGS += --no-print-directory
4141
DIR_DUP = mkdir -p $(@D)
4242

4343
#------------------------------------------------#

projects/v4/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ ARFLAGS := -r -c -s
3838
# UTENSILS #
3939
#------------------------------------------------#
4040
# RM force remove
41-
# MAKE quietly make
41+
# MAKEFLAGS make flags
4242
# DIR_DUP duplicate directory tree
4343

4444
RM := rm -f
45-
MAKE := $(MAKE) --no-print-directory
45+
MAKEFLAGS += --no-print-directory
4646
DIR_DUP = mkdir -p $(@D)
4747

4848
#------------------------------------------------#

projects/v5/Makefile

+5-2
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ LDLIBS := $(addprefix -l,$(LIBS))
5151
# UTENSILS #
5252
#------------------------------------------------#
5353
# RM force remove
54-
# MAKE quietly make
54+
# MAKEFLAGS make flags
5555
# DIR_DUP duplicate directory tree
5656

5757
RM := rm -f
58-
MAKE := $(MAKE) --silent --no-print-directory
58+
MAKEFLAGS += --silent --no-print-directory
5959
DIR_DUP = mkdir -p $(@D)
6060

6161
#------------------------------------------------#
@@ -99,6 +99,9 @@ re:
9999
$(MAKE) fclean
100100
$(MAKE) all
101101

102+
info-%:
103+
$(MAKE) --dry-run --always-make $* | grep -v "info"
104+
102105
#------------------------------------------------#
103106
# SPEC #
104107
#------------------------------------------------#

projects/v5/lib/libarom/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ ARFLAGS := -r -c -s
3737
# UTENSILS #
3838
#------------------------------------------------#
3939
# RM force remove
40-
# MAKE quietly make
40+
# MAKEFLAGS make flags
4141
# DIR_DUP duplicate directory tree
4242

4343
RM := rm -f
44-
MAKE := $(MAKE) --no-print-directory
44+
MAKEFLAGS += --no-print-directory
4545
DIR_DUP = mkdir -p $(@D)
4646

4747
#------------------------------------------------#

projects/v5/lib/libbase/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ ARFLAGS := -r -c -s
3737
# UTENSILS #
3838
#------------------------------------------------#
3939
# RM force remove
40-
# MAKE quietly make
40+
# MAKEFLAGS make flags
4141
# DIR_DUP duplicate directory tree
4242

4343
RM := rm -f
44-
MAKE := $(MAKE) --no-print-directory
44+
MAKEFLAGS += --no-print-directory
4545
DIR_DUP = mkdir -p $(@D)
4646

4747
#------------------------------------------------#

projects/v5/src/main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
int main(void)
44
{
5-
write (1, "\n ICECREAM\n", 18);
5+
write (1, "\n ICECREAM\n\n", 18);
66
write (1, " [From lib base]\n\n", 21);
77
base_milk();
88
base_water();
99
write (1, "\n [From lib arom]\n\n", 22);
1010
arom_coco();
1111
arom_cherry();
1212
write (1, "\n [From lib m]\n\n", 19);
13-
dprintf(2, "m: pow(2,2) = %f\n\n", pow(2,2));
13+
printf("m: pow(2,2) = %f\n\n", pow(2,2));
1414
return (0);
1515
}

0 commit comments

Comments
 (0)