Skip to content

Commit

Permalink
README: started call function section.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuno-Jesus committed Aug 2, 2023
1 parent 2c484d5 commit f9b02e1
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 6 deletions.
45 changes: 39 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1414,19 +1414,52 @@ Linux

</details>

<details>
<summary><h4>call</h4> - call your own defined functions</summary>

```
$(call variable,param,param,…)
```

- `variable` - the name of the function to call.
- `param` - strings that will serve as arguments of the function defined in `variable`.

If you're constantly writing the same complex expressions, you can define a function of your own and assign it the expression. When the times to expand the function, each `param` is assigned to the temporary variables `$(1)`, `$(2)`, etc. As for `$(0)` it receives the variable in `variable`.

<!--
Functions for generic purpose
$(shell command)
$(call variable,param,param,…)
-->
Let's assume you need to compile a few sub-Makefiles and echo a message to alert it has been done. Something like this:

```Makefile
all:
echo "Compiling folder-1"
$(MAKE) -C folder-1

echo "Compiling folder-2"
$(MAKE) -C folder-2

echo "Compiling folder-3"
$(MAKE) -C folder-3
```

The example below, saved on [code/26-call-example](code/26-call-example) refactors the Makefile above by defining an expression that holds both the `echo` and the `make` commands:

```Makefile
SUBFOLDERS = folder-1 folder-2 folder-3

define compile
echo "Compiling $(1)"
$(MAKE) -C $(1)
endef

all:
$(foreach f, $(SUBFOLDERS), $(call compile, $(f)))
```

</details>


------------------------------------------------------------------


# <a name="useful-topics">Useful Topics</a>

I don't think those topics fit either in the beginner's guide or in the advanced topics. However, I think they are useful to know and can be used to improve your Makefiles.
Expand Down
16 changes: 16 additions & 0 deletions code/26-call-example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SUBFOLDERS = folder-1 folder-2 folder-3

define compile
$(info Compiling $(1))
$(MAKE) -C $(1)
endef

all:
$(foreach folder, $(SUBFOLDERS), $(call compile,$(folder)))

clean:
$(MAKE) clean -C folder-1
$(MAKE) clean -C folder-2
$(MAKE) clean -C folder-3

.SILENT:
9 changes: 9 additions & 0 deletions code/26-call-example/folder-1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CC = cc
CFLAGS = -Wall -Werror -Wextra
FILE = hello-1

all:
$(CC) $(CFLAGS) $(FILE).c -o $(FILE).o

clean:
$(RM) $(FILE).o
19 changes: 19 additions & 0 deletions code/26-call-example/folder-1/hello-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hello-1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ncarvalh <ncarvalh@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/02 16:37:19 by ncarvalh #+# #+# */
/* Updated: 2023/08/02 16:37:39 by ncarvalh ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdio.h>

int main(void)
{
printf("hello-1\n");
return (0);
}
Binary file added code/26-call-example/folder-1/hello-1.o
Binary file not shown.
9 changes: 9 additions & 0 deletions code/26-call-example/folder-2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CC = cc
CFLAGS = -Wall -Werror -Wextra
FILE = hello-2

all:
$(CC) $(CFLAGS) $(FILE).c -o $(FILE).o

clean:
$(RM) $(FILE).o
19 changes: 19 additions & 0 deletions code/26-call-example/folder-2/hello-2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hello-2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ncarvalh <ncarvalh@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/02 16:37:19 by ncarvalh #+# #+# */
/* Updated: 2023/08/02 16:43:05 by ncarvalh ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdio.h>

int main(void)
{
printf("hello-2\n");
return (0);
}
Binary file added code/26-call-example/folder-2/hello-2.o
Binary file not shown.
9 changes: 9 additions & 0 deletions code/26-call-example/folder-3/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CC = cc
CFLAGS = -Wall -Werror -Wextra
FILE = hello-3

all:
$(CC) $(CFLAGS) $(FILE).c -o $(FILE).o

clean:
$(RM) $(FILE).o
19 changes: 19 additions & 0 deletions code/26-call-example/folder-3/hello-3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hello-3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ncarvalh <ncarvalh@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/02 16:37:19 by ncarvalh #+# #+# */
/* Updated: 2023/08/02 16:43:02 by ncarvalh ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdio.h>

int main(void)
{
printf("hello-3\n");
return (0);
}
Binary file added code/26-call-example/folder-3/hello-3.o
Binary file not shown.

0 comments on commit f9b02e1

Please sign in to comment.