Special Built-in Target Names

Special Built-in targets name are makefile special predefined targets and they start with a dot (.) and change how make behaves.

Most Common Special Targets

1. .PHONY

Marks targets as actions, not files.

.PHONY: clean

clean: rm -f *.o app

Most commonly used special target.

2. .DEFAULT

Executed when no rule exists for a target.

.DEFAULT: @echo "Don't know how to build $@"

Example:

make abc

Output:

Don't know how to build abc

3. .PRECIOUS

Prevents Make from deleting a partially built target when interrupted.

.PRECIOUS: %.o

Useful for large builds.

4. .INTERMEDIATE

Marks files as temporary intermediate files.

.INTERMEDIATE: temp.o

Make may delete them after the build.

5. .SECONDARY

Like .INTERMEDIATE, but prevents deletion.

.SECONDARY: temp.o

6. .DELETE_ON_ERROR

Delete target if command fails.

.DELETE_ON_ERROR:

Example:

output: some_command_that_might_fail > output

Without this, a corrupted output file might remain.

7. .IGNORE

Ignore errors.

.IGNORE:

or

.IGNORE: clean

Equivalent to prefixing commands with:

-rm file

8. .SILENT

Suppress command echoing.

.SILENT:

Equivalent to writing @ before every command.

Legacy & Suffix Rule Targets

1. .LOW_RESOLUTION_TIME

Used for filesystems with poor timestamp resolution.

.LOW_RESOLUTION_TIME: generated_file

Rare today.

2. .SUFFIXES

Defines suffix rules.

.SUFFIXES: .c .o

Clear all suffixes:

.SUFFIXES:

Useful for speeding up Make.

  1. .SECONDEXPANSION

Enable second expansion of prerequisites.

.SECONDEXPANSION:

Advanced feature used in complex Makefiles.

Example:

.SECONDEXPANSION:

target: $$(VAR)

Behavior & Configuration Control

1. .EXPORT_ALL_VARIABLES

Export all Make variables to child processes.

.EXPORT_ALL_VARIABLES:

Equivalent to:

export

for every variable.

2. .NOTPARALLEL

Disable parallel execution.

.NOTPARALLEL:

Even if user runs:

make -j8

Make executes sequentially.

3. .ONESHELL

Run all recipe lines in one shell.

Without:

target: cd src pwd

Output:

/current

because each line gets a new shell.

With:

.ONESHELL:

target: cd src pwd

Output:

/current/src

Very useful.

4. .POSIX

Request POSIX-compatible behavior.

.POSIX:

Rarely used.

5. .DEFAULT_GOAL

Choose default target.

.DEFAULT_GOAL := app

Now:

make

builds app.


©2023-2024 rculock.com