The Linux make utility works by reading and interpreting a makefile. Typically you run make by simply typing the following command at the shell prompt:
make
When run this way, GNU make looks for a file named GNUmakefile, makefile, or Makefile — in that order. If make finds one of these makefiles, it builds the first target specified in that makefile. However, if make doesn’t find an appropriate makefile, it displays the following error message and exits:
make: *** No targets specified and no makefile found. Stop.
If your makefile happens to have a different name from the default names, you have to use the -f option to specify the makefile. The syntax of the make command with this option is
make -f <i>filename</i>
where filename is the name of the makefile.
Even when you have a makefile with a default name such as Makefile, you may want to build a specific target out of several targets defined in the makefile. In that case, you have to use the following syntax when you run make:
make target
For example, if the makefile contains the target named clean, you can build that target with this command:
make clean
Another special syntax overrides the value of a make variable. For example, GNU make uses the CFLAGS variable to hold the flags used when compiling C files. You can override the value of this variable when you invoke make. Here’s an example of how you can define CFLAGS as the option -g -O2:
make CFLAGS="-g -O2"
In addition to these options, GNU make accepts several other command-line options. This table lists the GNU make options.
Option | Meaning |
---|---|
-b | Ignores the variable given but accepts that variable for compatibility with other versions of make. |
-C DIR | Changes to the specified directory before reading the makefile. |
-d | Prints debugging information. |
-e | Allows environment variables to override definitions of similarly named variables in the makefile. |
-f FILE | Reads FILE as the makefile. |
-h | Displays the list of make options. |
-i | Ignores all errors in commands executed when building a target. |
-I DIR | Searches the specified directory for included makefiles. (The capability to include a file in a makefile is unique to GNU make.) |
-j NUM | Specifies the number of commands that make can run simultaneously. |
-k | Continues to build unrelated targets, even if an error occurs when building one of the targets. |
-l LOAD | Doesn’t start a new job if load average is at least LOAD (a floating-point number). |
-m | Ignores the variable given but accepts that variable for compatibility with other versions of make. |
-n | Prints the commands to execute but does not execute them. |
-o FILE | Does not rebuild the file named FILE, even if it is older than its dependents. |
-p | Displays the make database of variables and implicit rules. |
-q | Does not run anything, but returns 0 (zero) if all targets are up to date, 1 if anything needs updating, or 2 if an error occurs. |
-r | Gets rid of all built-in rules. |
-R | Gets rid of all built-in variables and rules. |
-s | Works silently (without displaying the commands as they execute). |
-t | Changes the timestamp of the files. |
-v | Displays the version number of make and a copyright notice. |
-w | Displays the name of the working directory before and after processing the makefile. |
-W FILE | Assumes that the specified file has been modified (used with -n to see what happens if you modify that file). |