1.11. Build using a Makefile

Now that we have learned compilation using command line interface, let us introduce Makefiles. To compile exactly the same way as we have compiled so far, a very simple Makefile (incomplete but still functional) would be as under.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
greeting_en:
	@echo Generating greeting_en from main.c and greeting_en.c
	gcc main.c greeting_en.c -Wall -o greeting_en

greeting_fr:
	@echo Generating greeting_fr from main.c and greeting_fr.c
	gcc main.c greeting_fr.c -Wall -o greeting_fr

greeting_es:
	@echo Generating greeting_es from main.c and greeting_es.c
	gcc main.c greeting_es.c -Wall -o greeting_es

In line nos. 1, 5 and 9 we are introducing Makefile targets. greeting_en is one such target. Now, we can run make greeting_en, make greeting_fr and make greeting_es.

Now let us inspect the target greeting_en.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
greeting_en:
	@echo Generating greeting_en from main.c and greeting_en.c
	gcc main.c greeting_en.c -Wall -o greeting_en

greeting_fr:
	@echo Generating greeting_fr from main.c and greeting_fr.c
	gcc main.c greeting_fr.c -Wall -o greeting_fr

greeting_es:
	@echo Generating greeting_es from main.c and greeting_es.c
	gcc main.c greeting_es.c -Wall -o greeting_es

When we introduced target greeting_en in line no. 1, line nos. 2 and 3 describe what has to happen when someone invokes make greeting_en. It may not be visible to naked eyes, but it is very important that those lines have to start with a TAB.

The @ in line no. 2 is a special make prefix that ensures that the command itself is not printed during execution. For more see Recipe Echoing in https://www.gnu.org/software/make/manual/html_node/Echoing.html

1.11.1. Compilation only for English

As we did in previous section Compilation only for English, to compile binary just for English language, the command would be.

make greeting_en

When the above command completes successfully, the compilation output would be:

Generating greeting_en from main.c and greeting_en.c
gcc main.c greeting_en.c -Wall -o greeting_en

Since there is @ to silence echo, we only see the output of the echo command, but not it’s execution. On the contrary, we see the command for invocation of gcc.

1.11.2. Compilation for all languages

As we did in previous section Compilation for all languages, to compile binary for all English language, the command would be.

Note

Doing it this way is no better approach than invoking gcc individually, but we will extend the make file in next section Makefile with more default convention.

make greeting_en
make greeting_fr
make greeting_es

When the above command completes successfully, the compilation output would be:

Generating greeting_en from main.c and greeting_en.c
gcc main.c greeting_en.c -Wall -o greeting_en
Generating greeting_fr from main.c and greeting_fr.c
gcc main.c greeting_fr.c -Wall -o greeting_fr
Generating greeting_es from main.c and greeting_es.c
gcc main.c greeting_es.c -Wall -o greeting_es

To run, as of now we will use same old approach as invoking each file individually:

./greeting_en
./greeting_fr
./greeting_es

And then we should see the same expected output:

Hello World!
Bonjour le monde!
Hola Mundo!