Substitution references

Substitution references in GNU make provide a shorthand way to alter the value of a variable by replacing parts of its text.

They are most commonly used to transform a list of source files into a list of object files.

There are two primary types of substitution references

  1. suffix substitution
  2. pattern substitution

1. suffix substitution

  • Syntax:

    $(var:old_suffix=new_suffix)
    

    var: The name of the variable to modify (do not use $ here).

    old_suffix: The text at the end of a word to be replaced.

    new_suffix: The text that replaces the old suffix.

  • Example:

    SRC = main.c utils.c
    OBJ = $(SRC:.c=.o)
    

2. pattern substitution

  • Syntax:

    $(var:pattern=replacement)
    

    var: The name of the variable to modify (do not use $ here)

    pattern: the pattern need to be replaced

    replacement: the text patten that replaces the old pattern

  • Example:

    SRC = src/main.c src/utils.c
    OBJ = $(SRC:%.c=%.o)
    




©2023-2024 rculock.com