### Install Program Command Source: https://www.gnu.org/software/make/manual/html_node/Command-Variables.html Use the `INSTALL_PROGRAM` variable to install executable files. This is a minimal example. ```makefile $(INSTALL_PROGRAM) foo $(bindir)/foo ``` -------------------------------- ### Install-Strip Target Example Source: https://www.gnu.org/software/make/manual/html_node/Standard-Targets.html This example shows how to implement an 'install-strip' target that uses the 'install' target but with the '-s' flag to strip executable files during installation. It's a common pattern for reducing executable size. ```makefile install-strip: $(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' \ install ``` -------------------------------- ### Example make command with DESTDIR Source: https://www.gnu.org/software/make/manual/make.txt Shows how a user would invoke make with the DESTDIR variable to perform a staged installation into a temporary directory. ```bash make DESTDIR=/tmp/stage install ``` -------------------------------- ### Create Installation Directories with mkinstalldirs Source: https://www.gnu.org/software/make/manual/html_node/Standard-Targets.html Use this snippet to create standard installation directories like bindir, datadir, etc. Ensure these directories exist before installation. ```makefile # Make sure all installation directories (e.g. $(bindir)) # actually exist by making them if necessary. installdirs: mkinstalldirs $(srcdir)/mkinstalldirs $(bindir) $(datadir) \ $(libdir) $(infodir) \ $(mandir) ``` -------------------------------- ### Create Installation Directories with Make Source: https://www.gnu.org/software/make/manual/make.txt Rule to ensure all necessary installation directories exist. It uses the 'mkinstalldirs' script and supports the DESTDIR variable for staging installations. ```makefile # Make sure all installation directories (e.g. $(bindir)) # actually exist by making them if necessary. installdirs: mkinstalldirs $(srcdir)/mkinstalldirs $(bindir) $(datadir) \ $(libdir) $(infodir) \ $(mandir) ``` ```makefile # Make sure all installation directories (e.g. $(bindir)) # actually exist by making them if necessary. installdirs: mkinstalldirs $(srcdir)/mkinstalldirs \ $(DESTDIR)$(bindir) $(DESTDIR)$(datadir) \ $(DESTDIR)$(libdir) $(DESTDIR)$(infodir) \ $(DESTDIR)$(mandir) ``` -------------------------------- ### Install Data Command Source: https://www.gnu.org/software/make/manual/html_node/Command-Variables.html Use the `INSTALL_DATA` variable to install non-executable files like libraries. This is a minimal example. ```makefile $(INSTALL_DATA) libfoo.a $(libdir)/libfoo.a ``` -------------------------------- ### Basic Make Rule Example Source: https://www.gnu.org/software/make/manual/html_node/Rule-Example.html This rule defines how to build 'foo.o' from 'foo.c' and 'defs.h'. The recipe starts with a tab character. ```makefile foo.o : foo.c defs.h # module for twiddling the frobs cc -c -g foo.c ``` -------------------------------- ### Installation Target Source: https://www.gnu.org/software/make/manual/html_node/Complex-Makefile.html Installs the 'tar' executable, the 'rmt' program (if it exists), and the info documentation files to their respective locations. ```makefile .PHONY: install install: all $(INSTALL) tar $(bindir)/$(binprefix)tar -test ! -f rmt || $(INSTALL) rmt /etc/rmt $(INSTALLDATA) $(srcdir)/tar.info* $(infodir) ``` -------------------------------- ### Use INSTALL_PROGRAM and INSTALL_DATA for Installation Source: https://www.gnu.org/software/make/manual/make.txt Define and use INSTALL_PROGRAM and INSTALL_DATA variables for installing executables and data files respectively. Defaults are provided, and these variables should be used for actual installation commands. ```makefile $(INSTALL_PROGRAM) foo $(bindir)/foo $(INSTALL_DATA) libfoo.a $(libdir)/libfoo.a ``` -------------------------------- ### Installation Paths Configuration Source: https://www.gnu.org/software/make/manual/html_node/Complex-Makefile.html Sets the installation prefix and directories for executables and info files. ```makefile prefix = /usr/local # Prefix for each installed program, # normally empty or `g'. binprefix = # The directory to install tar in. bindir = $(prefix)/bin # The directory to install the info files in. infodir = $(prefix)/info ``` -------------------------------- ### Creating Installation Directories with Make Source: https://www.gnu.org/software/make/manual/html_node/Standard-Targets.html A suggested rule for the 'installdirs' target, which creates necessary directories for installation. It leverages the 'mkinstalldirs' script. ```makefile installdirs: $(mkinstalldirs) $(bindir) $(libdir) $(infodir) ``` -------------------------------- ### Basic Make Rule Example Source: https://www.gnu.org/software/make/manual/make.txt A simple rule showing a target, its prerequisites, and the command to build it. The recipe line must start with a tab. ```makefile foo.o : foo.c defs.h # module for twiddling the frobs cc -c -g foo.c ``` -------------------------------- ### Install Multiple Files Source: https://www.gnu.org/software/make/manual/html_node/Command-Variables.html It is acceptable to install multiple files in a single command by listing them before the target directory. ```makefile $(INSTALL_PROGRAM) foo bar baz $(bindir) ``` -------------------------------- ### Example: Loading with Custom Initialization Function Source: https://www.gnu.org/software/make/manual/html_node/load-Directive.html This example loads the dynamic object `../mk_funcs.so` and explicitly calls the initialization function `init_mk_func`. ```makefile load ../mk_funcs.so(init_mk_func) ``` -------------------------------- ### Install Command Categories Source: https://www.gnu.org/software/make/manual/html_node/Install-Command-Categories.html These are the category lines for the 'install' target. They specify the category for the commands that follow. Use these to classify commands into pre-install, post-install, or normal installation. ```makefile $(PRE_INSTALL) # Pre-install commands follow. $(POST_INSTALL) # Post-install commands follow. $(NORMAL_INSTALL) # Normal commands follow. ``` -------------------------------- ### Example: Loading an Object File Source: https://www.gnu.org/software/make/manual/html_node/load-Directive.html This example demonstrates loading a dynamic object file named `../mk_funcs.so`. GNU `make` will automatically look for and invoke the initialization function `mk_funcs_gmk_setup`. ```makefile load ../mk_funcs.so ``` -------------------------------- ### Simple Makefile Example Source: https://www.gnu.org/software/make/manual/make.txt Defines rules for compiling an executable 'edit' from object files and cleaning up generated files. Ensure recipes start with a tab character. ```makefile edit : main.o kbd.o command.o display.o \ insert.o search.o files.o utils.o cc -o edit main.o kbd.o command.o display.o \ insert.o search.o files.o utils.o main.o : main.c defs.h cc -c main.c kbd.o : kbd.c defs.h command.h cc -c kbd.c command.o : command.c defs.h command.h cc -c command.c display.o : display.c defs.h buffer.h cc -c display.c insert.o : insert.c defs.h buffer.h cc -c insert.c search.o : search.c defs.h buffer.h cc -c search.c files.o : files.c defs.h buffer.h command.h cc -c files.c utils.o : utils.c defs.h cc -c utils.c clean : rm edit main.o kbd.o command.o display.o \ insert.o search.o files.o utils.o ``` -------------------------------- ### GNU Tar Makefile Example Source: https://www.gnu.org/software/make/manual/make.txt This is a complex Makefile example for the GNU 'tar' program. It includes a shebang line for direct execution and defines various targets for building, installing, and cleaning the project. It also includes system configuration variables and preprocessor definitions. ```makefile #!/usr/bin/make -f # Generated automatically from Makefile.in by configure. # Un*x Makefile for GNU tar program. # Copyright (C) 1991 Free Software Foundation, Inc. # This program is free software; you can redistribute # it and/or modify it under the terms of the GNU # General Public License ... ... ... SHELL = /bin/sh #### Start of system configuration section. #### srcdir = . # If you use gcc, you should either run the # fixincludes script that comes with it or else use # gcc with the -traditional option. Otherwise ioctl # calls will be compiled incorrectly on some systems. CC = gcc -O YACC = bison -y INSTALL = /usr/local/bin/install -c INSTALLDATA = /usr/local/bin/install -c -m 644 # Things you might add to DEFS: # -DSTDC_HEADERS If you have ANSI C headers and # libraries. # -DPOSIX If you have POSIX.1 headers and # libraries. # -DBSD42 If you have sys/dir.h (unless # you use -DPOSIX), sys/file.h, # and st_blocks in `struct stat'. # -DUSG If you have System V/ANSI C # string and memory functions # and headers, sys/sysmacros.h, # fcntl.h, getcwd, no valloc, # and ndir.h (unless # you use -DDIRENT). # -DNO_MEMORY_H If USG or STDC_HEADERS but do not # include memory.h. # -DDIRENT If USG and you have dirent.h # instead of ndir.h. # -DSIGTYPE=int If your signal handlers # return int, not void. # -DNO_MTIO If you lack sys/mtio.h # (magtape ioctls). # -DNO_REMOTE If you do not have a remote shell # or rexec. # -DUSE_REXEC To use rexec for remote tape # operations instead of # forking rsh or remsh. # -DVPRINTF_MISSING If you lack vprintf function # (but have _doprnt). # -DDOPRNT_MISSING If you lack _doprnt function. # Also need to define # -DVPRINTF_MISSING. # -DFTIME_MISSING If you lack ftime system call. # -DSTRSTR_MISSING If you lack strstr function. # -DVALLOC_MISSING If you lack valloc function. # -DMKDIR_MISSING If you lack mkdir and # rmdir system calls. # -DRENAME_MISSING If you lack rename system call. # -DFTRUNCATE_MISSING If you lack ftruncate # system call. # -DV7 On Version 7 Unix (not # tested in a long time). # -DEMUL_OPEN3 If you lack a 3-argument version # of open, and want to emulate it # with system calls you do have. ``` -------------------------------- ### Support DESTDIR for Staged Installs Source: https://www.gnu.org/software/make/manual/make.txt Illustrates how to prepend the DESTDIR variable to target file paths for staged installations. DESTDIR should be specified on the make command line. ```makefile $(INSTALL_PROGRAM) foo $(DESTDIR)$(bindir)/foo $(INSTALL_DATA) libfoo.a $(DESTDIR)$(libdir)/libfoo.a ``` -------------------------------- ### Create Installation Directories with DESTDIR Support Source: https://www.gnu.org/software/make/manual/html_node/Standard-Targets.html This variant supports the DESTDIR variable, which is crucial for staging installations. It ensures that directories are created relative to the DESTDIR path. ```makefile # Make sure all installation directories (e.g. $(bindir)) # actually exist by making them if necessary. installdirs: mkinstalldirs $(srcdir)/mkinstalldirs \ $(DESTDIR)$(bindir) $(DESTDIR)$(datadir) \ $(DESTDIR)$(libdir) $(DESTDIR)$(infodir) \ $(DESTDIR)$(mandir) ``` -------------------------------- ### Conditional Install of Info Files Source: https://www.gnu.org/software/make/manual/html_node/Standard-Targets.html This snippet demonstrates how to conditionally install Info files by checking if the 'install-info' command is available. It ensures that 'install-info' is only called if it exists, otherwise, it does nothing. ```makefile $(POST_INSTALL) if $(SHELL) -c 'install-info --version' \ >/dev/null 2>&1; then \ install-info --dir-file="$(DESTDIR)$(infodir)/dir" \ "$(DESTDIR)$(infodir)/foo.info"; \ else true; fi ``` -------------------------------- ### Strip Executables During Installation Source: https://www.gnu.org/software/make/manual/make.txt This target uses the 'install' target to install programs, applying the '-s' flag to strip symbols from executables. It's a common pattern for optimizing installed binaries. ```makefile install-strip: $(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' \ install ``` -------------------------------- ### Install Info File Rule in Make Source: https://www.gnu.org/software/make/manual/make.txt This rule installs an Info file and attempts to handle cases where 'install-info' might not be present. It prioritizes local Info files over those in 'srcdir' and uses 'SHELL -c' for robust execution of 'install-info'. ```make do-install-info: foo.info installdirs $(NORMAL_INSTALL) # Prefer an info file in . to one in srcdir. if test -f foo.info; then d=.; \ else d="$(srcdir)"; fi; \ $(INSTALL_DATA) $$d/foo.info \ "$(DESTDIR)$(infodir)/foo.info" # Run install-info only if it exists. # Use 'if' instead of just prepending '-' to the # line so we notice real errors from install-info. # Use '$(SHELL) -c' because some shells do not # fail gracefully when there is an unknown command. $(POST_INSTALL) if $(SHELL) -c 'install-info --version' >/dev/null 2>&1; then \ install-info --dir-file="$(DESTDIR)$(infodir)/dir" "$(DESTDIR)$(infodir)/foo.info"; \ else true; fi ``` -------------------------------- ### Install Info File Rule Source: https://www.gnu.org/software/make/manual/html_node/Standard-Targets.html This rule installs an Info file and attempts to handle cases where 'install-info' might not be present. It prioritizes local Info files over those in srcdir and uses '$(INSTALL_DATA)' for copying. ```makefile do-install-info: foo.info installdirs $(NORMAL_INSTALL) # Prefer an info file in . to one in srcdir. if test -f foo.info; then d=.; \ else d="$(srcdir)"; fi; \ $(INSTALL_DATA) $$d/foo.info \ "$(DESTDIR)$(infodir)/foo.info" # Run install-info only if it exists. # Use 'if' instead of just prepending '-' to the # line so we notice real errors from install-info. # Use '$(SHELL) -c' because some shells do not ``` -------------------------------- ### Example: Creating a Directory with Order-Only Prerequisite Source: https://www.gnu.org/software/make/manual/html_node/Prerequisite-Types.html This example demonstrates using an order-only prerequisite to ensure a directory is created before any object files are built into it. The directory itself is an order-only prerequisite, preventing target rebuilds due to directory timestamp changes. ```makefile OBJDIR := objdir OBJS := $(addprefix $(OBJDIR)/,foo.o bar.o baz.o) $(OBJDIR)/%.o : %.c $(COMPILE.c) $(OUTPUT_OPTION) $< all: $(OBJS) $(OBJS): | $(OBJDIR) $(OBJDIR): mkdir $(OBJDIR) ``` -------------------------------- ### Generating Info Files with Make Source: https://www.gnu.org/software/make/manual/html_node/Standard-Targets.html Example rule for generating Info files. Requires the MAKEINFO variable to be defined, which should execute the 'makeinfo' program. ```makefile info: foo.info foo.info: foo.texi chap1.texi chap2.texi $(MAKEINFO) $(srcdir)/foo.texi ``` -------------------------------- ### Using DESTDIR with INSTALL_PROGRAM and INSTALL_DATA Source: https://www.gnu.org/software/make/manual/html_node/DESTDIR.html Demonstrates how to prepend the DESTDIR variable to target file paths when using INSTALL_PROGRAM and INSTALL_DATA for staged installs. ```makefile $(INSTALL_PROGRAM) foo $(DESTDIR)$(bindir)/foo $(INSTALL_DATA) libfoo.a $(DESTDIR)$(libdir)/libfoo.a ``` -------------------------------- ### Manual Prerequisite Rule Example Source: https://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html This is a manual rule showing that main.o depends on defs.h. Such rules are tedious to maintain for large projects. ```makefile main.o: defs.h ``` -------------------------------- ### Compiler -M Output Example Source: https://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html The output of `cc -M main.c` lists the object file and its dependencies, including source and header files. ```makefile main.o : main.c defs.h ``` -------------------------------- ### Invoking Make with DESTDIR Source: https://www.gnu.org/software/make/manual/html_node/DESTDIR.html Shows how to specify the DESTDIR variable on the make command line to perform a staged installation into a temporary directory. ```bash make DESTDIR=/tmp/stage install ``` -------------------------------- ### Extracting Pre-installation Commands with Make and Gawk Source: https://www.gnu.org/software/make/manual/make.txt This command-line example demonstrates how to extract pre-installation commands for binary package creation. It uses 'make' with specific variable assignments and pipes the output to 'gawk' for filtering. ```bash make -s -n install -o all \ PRE_INSTALL=pre-install \ POST_INSTALL=post-install \ NORMAL_INSTALL=normal-install \ | gawk -f pre-install.awk ``` -------------------------------- ### Get GNU Make Version Source: https://www.gnu.org/software/make/manual/html_node/Bugs.html Use this command to retrieve the version of GNU Make currently installed. Include this information when reporting bugs. ```bash make --version ``` -------------------------------- ### Use 'shell' Function to Get File Contents in Make Source: https://www.gnu.org/software/make/manual/make.txt Use the 'shell' function to capture the output of a shell command. This example sets the 'contents' variable to the content of 'foo', with lines separated by spaces. ```makefile contents := $(shell cat foo) ``` -------------------------------- ### Equivalent to Basic File Finding Source: https://www.gnu.org/software/make/manual/html_node/Foreach-Function.html This example shows an alternative way to achieve the same result as the previous snippet, by explicitly listing all wildcard expansions. It serves as a comparison to illustrate the conciseness of the foreach function. ```makefile files := $(wildcard a/* b/* c/* d/*) ``` -------------------------------- ### Syntax of the `let` Function Source: https://www.gnu.org/software/make/manual/html_node/Let-Function.html Illustrates the general syntax for using the `let` function, including variable assignments and the text to be expanded. ```makefile $(let var [var ...],[list],text) ``` -------------------------------- ### Makefile to Compile C Files Source: https://www.gnu.org/software/make/manual/html_node/Wildcard-Function.html A complete makefile example that uses `wildcard` and `patsubst` to dynamically determine C source files and their corresponding object files, then links them. ```makefile objects := $(patsubst %.c,%.o,$(wildcard *.c)) foo : $(objects) cc -o foo $(objects) ``` -------------------------------- ### Conditional Compilation Example Source: https://www.gnu.org/software/make/manual/make.txt This example shows how to conditionally include different library flags based on the value of the CC variable. ```makefile foo: $(objects) $(CC) -o foo $(objects) $(libs_for_gcc) ``` ```makefile foo: $(objects) $(CC) -o foo $(objects) $(normal_libs) ``` -------------------------------- ### Directory Search for Link Libraries in Make Source: https://www.gnu.org/software/make/manual/html_node/Libraries_002fSearch.html This example demonstrates how Make handles a '-lcurses' prerequisite by searching for libcurses.so and libcurses.a. The command executed will link the found library. ```makefile foo : foo.c -lcurses cc $^ -o $@ ``` -------------------------------- ### Make Command for Extracting Pre-installation Commands Source: https://www.gnu.org/software/make/manual/html_node/Install-Command-Categories.html This command uses 'make' with specific options and 'gawk' to extract pre-installation commands. The -s option silences messages, -n prints commands without executing, and the variables PRE_INSTALL, POST_INSTALL, and NORMAL_INSTALL are overridden to help filter commands. ```bash make -s -n install -o all \ PRE_INSTALL=pre-install \ POST_INSTALL=post-install \ NORMAL_INSTALL=normal-install \ | gawk -f pre-install.awk ``` -------------------------------- ### Makefile Configuration Variables Source: https://www.gnu.org/software/make/manual/make.txt Defines essential variables for compiling and installing the tar program. Includes compiler flags, library paths, and installation directories. ```makefile DEFS = -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \ -DVPRINTF_MISSING -DBSD42 # Set this to rtapelib.o unless you defined NO_REMOTE, # in which case make it empty. RTAPELIB = rtapelib.o LIBS = DEF_AR_FILE = /dev/rmt8 DEFBLOCKING = 20 CDEBUG = -g CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \ -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \ -DDEFBLOCKING=$(DEFBLOCKING) LDFLAGS = -g prefix = /usr/local # Prefix for each installed program, # normally empty or `g'. binprefix = # The directory to install tar in. bindir = $(prefix)/bin # The directory to install the info files in. infodir = $(prefix)/info ``` -------------------------------- ### Shell and System Configuration Section Source: https://www.gnu.org/software/make/manual/html_node/Complex-Makefile.html Defines the shell to be used and sets up basic system configuration variables like the source directory. ```makefile SHELL = /bin/sh #### Start of system configuration section. #### srcdir = . ``` -------------------------------- ### Example: Setting CFLAGS for a Target and its Prerequisites Source: https://www.gnu.org/software/make/manual/html_node/Target_002dspecific.html This example demonstrates setting the CFLAGS variable to '-g' for the 'prog' target. This value is then inherited by 'prog.o', 'foo.o', and 'bar.o', and their respective prerequisites. ```makefile prog : CFLAGS = -g prog : prog.o foo.o bar.o ``` -------------------------------- ### Link Multiple Object Files Example Source: https://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html Example of linking multiple object files into an executable. Make automatically handles the compilation of .c files to .o files and then links them. ```makefile x: y.o z.o cc -c x.c -o x.o cc -c y.c -o y.o cc -c z.c -o z.o cc x.o y.o z.o -o x rm -f x.o rm -f y.o rm -f z.o ``` -------------------------------- ### Conditional Variable Assignment for Linking Source: https://www.gnu.org/software/make/manual/html_node/Conditional-Example.html This example shows how to conditionally assign a variable and then use it unconditionally. It achieves the same result as the previous example by setting the `libs` variable based on `CC`. ```makefile libs_for_gcc = -lgnu normal_libs = ifeq ($(CC),gcc) libs=$(libs_for_gcc) else libs=$(normal_libs) endif foo: $(objects) $(CC) -o foo $(objects) $(libs) ``` -------------------------------- ### Extract a range of words from text Source: https://www.gnu.org/software/make/manual/html_node/Text-Functions.html Extracts words from a text string, starting from the s-th word up to the e-th word (inclusive). Word indexing starts at 1. Handles out-of-bounds indices gracefully. ```makefile $(wordlist 2, 3, foo bar baz) ``` -------------------------------- ### Override with CFLAGS Example Source: https://www.gnu.org/software/make/manual/html_node/Override-Directive.html This example demonstrates how to ensure the '-g' switch is always included when compiling C code, even if the user provides other CFLAGS on the command line. It appends '-g' to any existing CFLAGS. ```makefile override CFLAGS += -g ``` -------------------------------- ### POSIX Shell Recipe with .ONESHELL Source: https://www.gnu.org/software/make/manual/make.txt This example shows a POSIX-style shell recipe that works as expected with .ONESHELL, demonstrating the removal of special prefix characters before processing. ```makefile .ONESHELL: foo : bar/lose @cd $(@D) @gobble $(@F) > ../$@ ``` -------------------------------- ### GNU Make Category Lines for Install Source: https://www.gnu.org/software/make/manual/make.txt Use these special variables in category lines to classify commands within 'install' rules. Commands following a category line are assigned to that category until the next category line. ```makefile $(PRE_INSTALL) # Pre-install commands follow. ``` ```makefile $(POST_INSTALL) # Post-install commands follow. ``` ```makefile $(NORMAL_INSTALL) # Normal commands follow. ``` -------------------------------- ### Basic Pattern Rule Example in Make Source: https://www.gnu.org/software/make/manual/html_node/Pattern-Intro.html Use this pattern rule to specify how to build a '.o' file from a corresponding '.c' file. The '%' in the target and prerequisite must match the same stem. ```makefile %.o : %.c ;recipe… ``` -------------------------------- ### Get Absolute File Name Without Resolving Symlinks Source: https://www.gnu.org/software/make/manual/html_node/File-Name-Functions.html Use $(abspath) to get an absolute path that does not contain '.' or '..' components or repeated separators. Unlike realpath, it does not resolve symlinks and does not require the file to exist. ```makefile $(abspath names…) ``` -------------------------------- ### Makefile Recipe Line Splitting Examples Source: https://www.gnu.org/software/make/manual/make.txt Demonstrates how 'make' interprets backslash-newline pairs for splitting recipe lines. The output varies based on shell interpretation and quoting. ```makefile all : @echo no\ space @echo no\ space @echo one \ space @echo one\ space ``` ```makefile all : ; @echo 'hello \ world' ; echo "hello \ world" ``` -------------------------------- ### Create Files Using Guile Functions in Make Source: https://www.gnu.org/software/make/manual/make.txt Utilize Guile functions within a makefile rule to create and write to files. This example demonstrates opening a file, writing each prerequisite to it, and then closing the file. ```makefile prog: $(PREREQS) @$(guile (mkopen "tmp.out" "w")) \ $(foreach X,$^,$(guile (mkwrite "$(X)"))) \ $(guile (mkclose)) $(LINK) < tmp.out ``` -------------------------------- ### Managing Default Goals with .DEFAULT_GOAL Source: https://www.gnu.org/software/make/manual/html_node/Special-Variables.html Illustrates how to query, reset, and set the default goal using the .DEFAULT_GOAL special variable. Use this to control which target is executed when no target is specified on the command line. ```makefile # Query the default goal. ifeq ($(.DEFAULT_GOAL),) $(warning no default goal is set) endif .PHONY: foo foo: ; @echo $@ $(warning default goal is $(.DEFAULT_GOAL)) # Reset the default goal. .DEFAULT_GOAL := .PHONY: bar bar: ; @echo $@ $(warning default goal is $(.DEFAULT_GOAL)) ``` -------------------------------- ### Example of Appending to a Variable Source: https://www.gnu.org/software/make/manual/html_node/Appending.html Demonstrates how '+=' modifies a variable's value by appending new text. ```makefile objects = main.o foo.o bar.o utils.o objects += another.o ``` -------------------------------- ### Portable Utilities for Makefiles Source: https://www.gnu.org/software/make/manual/html_node/Utilities-in-Makefiles.html These utilities are generally safe to use in Makefiles for building and installation. Avoid non-standard options or features. ```shell awk cat cmp cp diff echo expr false grep install-info ln ls mkdir mv printf pwd rm rmdir sed sleep sort tar test touch tr true ``` -------------------------------- ### Build and Installation Programs via Make Variables Source: https://www.gnu.org/software/make/manual/html_node/Utilities-in-Makefiles.html Compilers and related programs should be invoked via make variables to allow user substitution. Ensure these variables are defined. ```shell ar bison cc flex install ld ldconfig lex make makeinfo ranlib texi2dvi yacc ``` -------------------------------- ### Simply Expanded Variable Assignment Example Source: https://www.gnu.org/software/make/manual/html_node/Simple-Assignment.html Demonstrates the behavior of simply expanded variables where values are expanded at definition time. ```makefile x := foo y := $(x) bar x := later ``` -------------------------------- ### Generating DVI Files with Make Source: https://www.gnu.org/software/make/manual/html_node/Standard-Targets.html Example rule for generating DVI files from Texinfo. Requires the TEXI2DVI variable to be defined, which should execute the 'texi2dvi' program. ```makefile dvi: foo.dvi foo.dvi: foo.texi chap1.texi chap2.texi $(TEXI2DVI) $(srcdir)/foo.texi ```