Makefile running parameters

Makefile operating parameters

  • Review the past and learn the new
  • Specify Makefile parameters
  • Arguments for specified goals
  • Instead of executing the recipe
  • Avoid recompiling certain files
  • Override variables
  • Compilation of test programs
  • Temporary Files
  • Summary of options

Next article: Exercise 10: Implicit rules in Makefile, Previous article: Exercise 8: Using functions in Makefile, Table of Contents | Home Page

Review the past and learn the new

  • Exercise 1 Understand the basic process of compilation
  • Exercise 2 Create a basic Makefile
  • Exercise 3 A brief introduction to Makefile
  • Exercise 4 Explicit rules of Makefile
  • Exercise 5 Makefile recipe rules
  • Exercise 6 Makefile using variables
  • Exercise 7 Conditional part of Makefile
  • Exercise 8 Makefile using functions

The make tool can be used in a variety of ways, mainly depending on whether you provide arguments at run time. In its simplest form, if you run make without supplying any arguments, it will recompile all expired files. Typically, Makefiles are written so that running make without any arguments achieves this goal.

But you might want to update only some of them; you might want to use a different compiler or different compilation options; you might just want to find which files are out of date without making actual changes.

You can perform these operations and many others by supplying arguments when running make.

The exit status of make is always one of the following three values:

  • 0: If make succeeds, the exit status is zero.
  • 2: If make encounters any errors, the exit status is two. It will print a message describing the specific error.
  • 1: If the -q flag is used and make determines that a target has not been updated, the exit status is one.

Next article: Exercise 10: Implicit rules in Makefile, Previous article: Exercise 8: Using functions in Makefile, Table of Contents | Home Page

Specify Makefile parameters

In the make tool, you can pass the -f or --file option (--makefile can also be used) to specify the name of the Makefile. For example, -f altmake means to use the altmake file as the Makefile.

If you use the -f flag multiple times, and follow each -f with an argument, all specified files will be combined as a Makefile.

If you do not use the -f or --file flag, by default it will try to use GNUmakefile, makefile and Makefile, in this order, uses the first existing or createable file as the Makefile.

Next article: Exercise 10: Implicit rules in Makefile, Previous article: Exercise 8: Using functions in Makefile, Table of Contents | Home Page

Arguments for specifying the target

In the make tool, the target is the target that make should eventually strive to update. Other goals will also be updated if they serve as prerequisites for the goal, or are prerequisites for the goal’s prerequisites, and so on.

By default, the target is the first target in the Makefile (targets starting with a period are not counted). Therefore, makefiles are usually written such that the first target is used to compile the entire program or programs described. If the first rule in the Makefile has multiple targets, only the first target in the rule becomes the default target, not the entire list. You can manage the selection of the default target in the Makefile using the .DEFAULT_GOAL variable (see Other Special Variables).

You can also specify different targets via the make command line arguments. Take the target name as argument. If multiple targets are specified, make will process them in the order specified.

Any target in a Makefile can be specified as a target (unless it begins with - or contains =, in which case it will be parsed as a switch or variable definition ). Even targets that do not exist in the Makefile can be specified if make can find implicit rules describing how to make them.

make sets the special variable MAKECMDGOALS to the list of targets you specify on the command line. If no target is specified on the command line, this variable is empty. Note that this variable should only be used in special cases.

An example of appropriate use is to avoid including .d files in clean rules (see Generating Prerequisites Automatically) so that make does not create them , just delete them immediately:

sources = foo.c bar.c

ifeq (,$(filter clean,$(MAKECMDGOALS))
include $(sources:.c=.d)
endif

One use for specifying a target is if you only want to compile part of a program, or only one of multiple programs. Specify each file you want to remaster as a target. For example, consider a directory containing several programs where the makefile begins as follows:

.PHONY: all
all: size nm ld ar as

If you’re working with a size program, you might want to say make size so that only the program’s files are recompiled.

Another use for specifying targets is to produce files that are not normally produced. For example, there might be a file containing debug output, or a version of a program compiled specifically for testing that has a rule in the Makefile but is not a prerequisite for the default target.

Another use for specifying a target is to run a recipe associated with a dummy target or an empty target. Many Makefiles include a dummy target called clean, which removes everything except source files. This is only performed when you explicitly use make clean. The following is a typical list of virtual and empty target names.

  • all: Make all top-level targets known to the Makefile.
  • clean: Removes all files normally created by running make.
  • mostlyclean: Similar to clean, but may not remove some files that one would not normally want to recompile. For example, GCC’s mostlyclean target does not remove libgcc.a because recompiling it is rarely necessary and takes a long time.
  • distclean, realclean, clobber: Any of these goals may be defined to remove more than clean document. For example, this will remove configuration files or links that would normally be automatically created by make, even if the Makefile itself cannot create these files.
  • install: Copies the executable to the directory where users usually search for commands; copies any auxiliary files used by the executable to the directory where they will be looked for.
  • print: Print a list of changed source files.
  • tar: Create a tar file of source files.
  • shar: Create a shell archive (shar file) of the source file.
  • dist: Create a distribution file from the source file. This could be a tar file, a shar file, a compressed version of any of the above, or even several of the above.
  • TAGS: Update the tag table for this program.
  • check, test: Perform self-test on the program built by this Makefile.

Next article: Exercise 10: Implicit rules in Makefile, Previous article: Exercise 8: Using functions in Makefile, Table of Contents | Home Page

Instead of executing the recipe

In the make tool, there are options to specify what make does instead of updating the target.

  • -n, --just-print, --dry-run, --recon: This is a The “do not execute” operation causes make to print out the commands it needs to execute to bring the target up to date without actually executing them. Note that even when this flag is used, some recipes will still be executed. Additionally, any recipes that update the included Makefile will still be executed.

    make -n
    
  • -t, --touch: This is a “Touch” operation that marks targets as updated but does not actually change them. In other words, make pretends to update targets, but does not actually change their contents; only their modification times are updated.

    make -t
    
  • -q, --question: This is a “Question” operation, which silently checks whether the target has been updated, but does not execute the recipe; the exit code shows whether it is required Any updates.

    make -q
    
  • -W file, --what-if=file, --assume-new=file, --new-file= file: “What if” operation. Each -W flag is followed by a file name. make records the modification time of the given file as the current time, although the actual modification time remains unchanged. Can be used with the -n flag to see what happens if a specific file is modified.

    make -n -W file
    

Note that the -n, -t and -q options do not affect characters starting with or containing + characters The recipe line of the string $(MAKE) or ${MAKE}. Note that only lines containing + characters or the strings $(MAKE) or ${MAKE} will run regardless of these options how. Other lines in the rule will not run unless they begin with + or contain $(MAKE) or ${MAKE}.

The -t flag prevents virtual targets from being updated unless they start with + or contain $(MAKE) or ${MAKE} recipe line.

The -W flag provides two functions:

  • If you also use the -n or -q flags, you can see what make does if you modify certain files.
  • Without the -n or -q flags, the -W flag can indicate when make actually executes the recipe. code>make pretends that certain files have been modified without actually running the recipes for those files.

Note that options -p and -v allow you to obtain additional information about make or the Makefile being used.

Next article: Exercise 10: Implicit rules in Makefile, Previous article: Exercise 8: Using functions in Makefile, Table of Contents | Home Page

Avoid recompiling certain files

You can use the -t flag when you have modified a source file but do not want to recompile all files that depend on it. This flag tells make not to run the commands in the rule, but instead to mark the target as up-to-date by changing its last modified date. You can follow these steps:

  1. Use the command make to recompile source files that do need to be recompiled, making sure that the object files are up to date before starting.
  2. Make changes in the header file.
  3. Use the command make -t to mark all object files as up to date. Changes to the header file will not cause any recompilation the next time make is run.

If you have changed the header files by the time some files really need to be recompiled, it may be too late to do so. Instead, use the -o file flag, which marks the specified file as “old.” This means that the file itself will not be recreated, nor will other files be recreated as a result. Follow these steps:

  1. Use make -o headerfile to recompile source files that need to be compiled for reasons independent of a specific header file. If multiple header files are involved, use separate -o options for each header file.
  2. Use make -t to touch all object files.

Next article: Exercise 10: Implicit rules in Makefile, Previous article: Exercise 8: Using functions in Makefile, Table of Contents | Home Page

Override variables

An argument containing ‘=’ in a command line argument specifies the value of a variable: ‘v=x’ sets the value of variable v to x. If a value is specified in this way, all regular assignments to the same variable in the Makefile are ignored; we say that they are overridden by command-line arguments.

The most common way to use this feature is to pass additional flags to the compiler. For example, in a properly written Makefile, the variable CFLAGS is included in every rule that runs the C compiler, so a file named foo.c would be compiled like this:

cc -c $(CFLAGS) foo.c

Therefore, whatever value you set for CFLAGS will affect every compilation. The Makefile may specify the usual values for CFLAGS, for example:

CFLAGS=-g

You can override this value each time you run make. For example, if you execute ‘make CFLAGS=’-g -O”, then every C compilation will use ‘cc -c -g -O’.

The variable CFLAGS is just one of many standard variables so you can change them this way. Get the full list.

You can also write a Makefile to view other user-defined variables, allowing the user to control other aspects of the Makefile by changing those variables.

When overriding variables with command line arguments, you can define recursive expansion variables or simple expansion variables. The above example shows recursively expanding variables; to create a simple expansion variable, you can use ‘:=’ or ‘::=’ instead of ‘=’. However, it doesn’t matter which type of variable is created unless you want to include a variable reference or function call in the value you specify.

Makefiles have a way to change variables that are overridden by the user, using the override directive, which has the following syntax: override variable = value’.

Next article: Exercise 10: Implicit rules in Makefile, Previous article: Exercise 8: Using functions in Makefile, Table of Contents | Home Page

Compilation of test program

Normally, when an error occurs while executing a shell command, make immediately gives up and returns a non-zero status. No further rules will be enforced for any target. An error means that the target could not be rebuilt correctly, and make will report the error as soon as it learns about it.

This is usually not what you want when you are compiling a program you just modified. Instead, you might want make to try to compile every file it can try to show as many compilation errors as possible.

In this case you should use the -k’ or –keep-going’ flag. This tells make to continue considering other prerequisites for the pending target before giving up and returning a non-zero status, regenerating them if necessary. For example, if an error occurs while compiling one object file, ‘make -k’ will continue compiling other object files, even though it already knows that linking them will not be possible. In addition to continuing after a shell command fails, ‘make -k’ will continue if possible if it finds that a target or prerequisite file cannot be generated. This will always result in an error message, but without ‘-k’ it will be a fatal error.

The normal behavior of make assumes that your goal is to keep the target up to date; as soon as make learns that this is not possible, it may report a failure immediately. The ‘-k’ flag indicates that the real purpose is to test as much as possible the changes made in the program, perhaps to find several independent problems so that they can be corrected before the next attempt to compile. This is why Emacs’ M-x compile command passes the -k’ flag by default.

Next article: Exercise 10: Implicit rules in Makefile, Previous article: Exercise 8: Using functions in Makefile, Table of Contents | Home Page

Temporary files

In some cases, make needs to create its own temporary files. These files must not be disturbed during a make run, including all recursively called make instances.

If the environment variable MAKE_TMPDIR is set, then all temporary files created by make will be placed there.

If MAKE_TMPDIR is not set, the current operating system’s standard location will be used for temporary files. For POSIX systems, this will be the location set in the TMPDIR environment variable, otherwise the system’s default location (for example, /tmp) will be used. On Windows, first check TMP, then TEMP, then TMPDIR, and finally use the system default temporary file location.

Note that this directory must already exist, otherwise make will fail: make will not try to create it.

These variables cannot be set in the Makefile: GNU make must have access to this location before starting to read the Makefile.

Next article: Exercise 10: Implicit rules in Makefile, Previous article: Exercise 8: Using functions in Makefile, Table of Contents | Home Page

Option summary

This is a list of all options understood by the make command:

  • -b’, -m’: These options are ignored for compatibility with other versions of make.

  • -B’, –always-make’: Treat all targets as obsolete. Even if the status of its prerequisites is unclear, GNU make will continue to consider these targets and regenerate them all.

  • -C dir’, –directory=dir’: Switch to the directory dir before reading the Makefile. If there are multiple ‘-C’ options, each is interpreted relative to the previous one. Typically used with recursive calls to make.

  • -d’, –debug[=options]’: Print debugging information in addition to normal processing. The specific debugging level and type can be specified through parameters. For example, -d’ is equivalent to –debug=a’.

  • -e’, –environment-overrides’: Make variables from the environment take precedence over variables in the Makefile.

  • -E string’, –eval=string’: Evaluate string string as Makefile syntax.

  • -f file’, –file=file’, –makefile=file’: Read the file named file as a Makefile.

  • -h’, –help’: Prompt for options that make understands, and then exit.

  • -i’, –ignore-errors’: Ignore errors in all recipes executed when regenerating the file.

  • -I dir’, –include-dir=dir’: Specify the directory dir to search for included Makefiles.

  • -j [jobs]’, –jobs[=jobs]’: Specify the number of recipes (jobs) to run simultaneously.

  • –jobserver-style=[style]’: Select the job server style to use.

  • -k’, –keep-going’: Continue execution as much as possible after an error occurs.

  • ‘-l [load]’, ‘–load-average[=load]’, ‘–max-load[=load]’: Specifies that when there are other running jobs and the load average is at least When it is load, new recipes should not be started.

  • -L’, –check-symlink-times’: On systems that support symlinks, causes make to also consider the timestamps of symlinks.

  • -n’, –just-print’, –dry-run’, –recon’: Print recipes that will be executed, but do not execute them.

  • ‘-o file’, ‘–old-file=file’, ‘–assume-old=file’: Do not regenerate the file file, even if it is older than its prerequisites, and do not regenerate the file due to Change and regenerate anything.

  • -O[type]’, –output-sync[=type]’: Ensure that the complete output of each recipe is printed in an uninterrupted sequence.

  • -p’, –print-data-base’: Print the database (rules and variable values) generated after reading the Makefile.

  • -q’, –question’: “question mode”. Only an exit status is returned indicating whether the specified target is already up to date, needs to be rebuilt, or if an error was encountered.

  • -r’, –no-builtin-rules’: Eliminate use of built-in implicit rules.

  • -R’, –no-builtin-variables’: Eliminate the use of built-in rule-specific variables.

  • -s’, –silent’, –quiet’: Silent operation, do not print the executing recipe.

  • -S’, –no-keep-going’, –stop’: Cancel the effect of the -k’ option.

  • –shuffle[=mode]’: Enables random adjustment of antecedent relationships.

  • -t’, –touch’: Touch the file instead of running its recipe.

  • –trace’: Display trace information of make execution.

  • -v’, –version’: Print the version of the make program along with the copyright, author list, and disclaimer statement, and then exit.

  • -w’, –print-directory’: Print the included working directory before and after executing the Makefile.

  • –no-print-directory’: Disable printing of the working directory under -w.

  • -W file’, –what-if=file’, –new-file=file’, –assume-new=file’: Pretend that the target file has just been modified.

  • –warn-undefined-variables’: Warn whenever make sees a reference to an undefined variable.

Please note that this is a long list and the options listed above may not be available for all versions of make.

Next article: Exercise 10: Implicit rules in Makefile, Previous article: Exercise 8: Using functions in Makefile, Table of Contents | Home Page