What is make?
5 make examples
Syntax and Options
Related Commands
What is make?
While compiling large programs, if some change is done in any of the source/header files then compiling the whole source code again does not make any sense. The ‘make’ utility determines which pieces of the source code need to be recompiled and it issues commands to recompile only those pieces.
5 make Examples
1. Compiling a basic program through make
The make utility works on file named ‘makefile’. This file contains information in the form of:
Target : Dependencies [tab] command to compile the 'Target'
Lets take a basic example of the following code ‘hello.c’
#include<stdio.h> int main(void) { printf("\n Hello World!!!\n"); return 0; }
If the above program is to be compiled through make utility then the makefile would look like :
hello : hello.o gcc -Wall hello.o -o hello hello.o : hello.c gcc -c -Wall hello.c -o hello.o
We can use the following command to get the code compiled
$make
OR
$make hello
If a target is not specified along with ‘make’ then it parses the ‘makefile’ and assumes the first target it counters as the final target.
2. Compiling a relatively complex program through make
The above program was a simple one as it contained only one file. The essence of make utility lies with the source code that contains several files. Lets look at the example below :
//file1.c #include<stdio.h> #include"file2.h" void callback (int result) { printf("\n Result is : [%d] \n", result); } int main(void) { int a=0,b=0; printf("\n Enter two numbers to add: "); scanf("%d %d",&a, &b); add(a,b,callback); return 0; }
//file2.c #include<stdio.h> #include"file2.h" void add(int a, int b, void(*f)(int)) { int c = a+b; f(c); }
//file2.h void add(int a, int b, void (*f)(int));
The makefile for the above would look like :
file : file1.o file2.o gcc -Wall file2.o file1.o -o file file1.o : file1.c file2.h gcc -c -Wall file1.c -o file1.o file2.o : file2.c file2.h gcc -c -Wall file2.c -o file2.o
So when the command
$make file
is issued, the above code is compiled. Also, you will observe that once after the above source is completely compiled, if some change in any source file is done then command corresponding to only that source file is triggered. This means that the complete source code is not compiled again.
3. The name ‘makefile’ or ‘Makefile’
As already discussed, the make utility searches for a file named ‘makefile’. If that file is not found, it then searches for ‘Makefile’. So, if you have two files named ‘makefile’ and ‘Makefile’ then make utility would pick up ‘makefile’ for its execution.
Also, an option -f followed by a file name tells the make utility to use that file as the make file.
make -f my_makefile
4. Invoking make from some other directory
make utility can also be invoked from any other directory. In this case, an option ‘-C’ followed by a path would specify which directory to enter before searching for the ‘makefile’ and issuing commands. For example, I ran the make command from some other directory but used the ‘-C’ option to specify the directory in which the target ‘makefile’ was :
$ make -C ./practice/ hello make: Entering directory `/home/himanshu/practice' make: `hello' is up to date. make: Leaving directory `/home/himanshu/practice'
5. Silent invocation of make utility
Sometimes it is desired for make utility to not print any information that is it should execute silently. In that case, the -s option lets the make know that no information is to be spitted out.
make -s
Syntax and Options
make [ -f makefile ] [ options ] … [ targets ] …
Short Option | Long Option | Option Description |
---|---|---|
-b, -m | These options are ignored for compatibility with other versions of make. | |
-B | –always-make | Unconditionally make all targets. |
-C dir | –directory=dir | Change to directory dir before reading the makefiles or doing anything else. If multiple -C options are specified, each is interpreted relative to the previous one: -C / -C etc is equivalent to -C /etc. This is typically used with recursive invocations of make. |
-d | Print debugging information in addition to normal processing. The debugging information says which files are being considered for remaking, which file-times are being compared and with what results, which files actually need to be remade, which implicit rules are considered and which are applied—everything interesting about how make decides what to do. | |
–debug[=FLAGS] | Print debugging information in addition to normal processing. If the FLAGS are omitted, then the behavior is the same as if -d was specified. FLAGS may be a for all debugging output (same as using -d), b for basic debugging, v for more verbose basic debugging, i for showing implicit rules, j for details on invocation of commands, and m for debugging while remaking makefiles. | |
-e | –environment-overrides | Give variables taken from the environment precedence over variables from makefiles. |
-i | –ignore-errors | Ignore all errors in commands executed to remake files. |
-I dir | –include-dir=dir | Specifies a directory dir to search for included makefiles. If several -I options are used to specify several directories, the directories are searched in the order specified. Unlike the arguments to other flags of make, directories given with -I flags may come directly after the flag: -Idir is allowed, as well as -I dir. This syntax is allowed for compatibility with the C preprocessor’s -I flag. |
-j [jobs] | –jobs[=jobs] | Specifies the number of jobs (commands) to run simultaneously. If there is more than one -j option, the last one is effective. If the -j option is given without an argument, make will not limit the number of jobs that can run simultaneously. |
-k | –keep-going | Continue as much as possible after an error. While the target that failed, and those that depend on it, cannot be remade, the other dependencies of these targets can be processed all the same. |
-p | –print-data-base | Print the data base (rules and variable values) that results from reading the makefiles; then execute as usual or as otherwise specified. This also prints the version information given by the -v switch (see below). To print the data base without trying to remake any files, use make -p -f/dev/null. |
-S | –no-keep-going, –stop | Cancel the effect of the -k option. This is never necessary except in a recursive make where -k might be inherited from the top-level make via MAKEFLAGS or if you set -k in MAKEFLAGS in your environment |
-W file | –what-if=file, –new-file=file, –assume-new=file | Pretend that the target file has just been modified. When used with the -n flag, this shows you what would happen if you were to modify that file. Without -n, it is almost the same as running a touch command on the given file before running make, except that the modification time is changed only in the imagination of make. |
Related Commands
None
Comments on this entry are closed.
Hi.. In my UNIX server wall command is blocked. When I used the command it shows as “COMMAND NOT FOUND”. How I unblock and use the command.? Please help me anyone…
Respected Sir,
sorry but, actually i do not understand about make file..
i know, .c->.S->.o are file path…
i understand discription are.
but how it is that?
Ragrards,
Rahul Rasal