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.
1greeting_en:
2 @echo Generating greeting_en from main.c and greeting_en.c
3 gcc main.c greeting_en.c -Wall -o greeting_en
4
5greeting_fr:
6 @echo Generating greeting_fr from main.c and greeting_fr.c
7 gcc main.c greeting_fr.c -Wall -o greeting_fr
8
9greeting_es:
10 @echo Generating greeting_es from main.c and greeting_es.c
11 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
.
1greeting_en:
2 @echo Generating greeting_en from main.c and greeting_en.c
3 gcc main.c greeting_en.c -Wall -o greeting_en
4
5greeting_fr:
6 @echo Generating greeting_fr from main.c and greeting_fr.c
7 gcc main.c greeting_fr.c -Wall -o greeting_fr
8
9greeting_es:
10 @echo Generating greeting_es from main.c and greeting_es.c
11 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!
1.11.3. Anatomy of the Makefile - targets and rules¶
1greeting_en:
2 @echo Generating greeting_en from main.c and greeting_en.c
3 gcc main.c greeting_en.c -Wall -o greeting_en
greeting_en
is the target. Rest are the rules to fulfill that target.
In simpler words, it can be said that, there must be a file called greeting_en
. If that file is not found, follow the rules.
Warning
The rules are prefixed with a TAB
, not 4 or 8 white spaces.