Linux makefile assignment operators

This info copied from: https://vuanhduy.wordpress.com/2014/05/02/the-variants-of-gnu-makefile-assignment-operation/

The differences in =, :=, ?= and += when assigning a macro in Makefile.

  • = operation
  • If the variable does not have a value, the operation assign the value to it
    If the variable already had a value, it is replaced.
    This value will be expanded when the variable is used.

    1. HELLO = world
    2. HELLO_WORLD = $(HELLO) world!
    3. # This echoes "world world!"
    4. echo $(HELLO_WORLD)
    5. HELLO = hello
    6. # This echoes "hello world!"
    7. echo $(HELLO_WORLD)

  • := operation
  • It is similar to the = operation but the value will be expanded during the assignment.

    1. HELLO = world
    2. HELLO_WORLD := $(HELLO) world!
    3. # This echoes "world world!"
    4. echo $(HELLO_WORLD)
    5. HELLO = hello
    6. # Still echoes "world world!"
    7. echo $(HELLO_WORLD)
    8. HELLO_WORLD := $(HELLO) world!
    9. # This echoes "hello world!"
    10. echo $(HELLO_WORLD)

  • += operation
  • Similar to the = operation but instead of replacing the value, the value is appended to the current one, with a space in between.

    1. HELLO_WORLD = hello
    2. HELLO_WORLD += world!
    3. # This echoes "hello world!"
    4. echo $(HELLO_WORLD)

  • ?= operation
  • It is similar to the = operation but only if the variable doesn’t have a value.

    1. HELLO_WORLD ?= hello world!
    2. # This echoes "hello world!"
    3. echo $(HELLO_WORLD)
    4. HELLO_WORLD ?= hi world!
    5. # Still echoes "hello world!"

Leave a Reply

Your email address will not be published. Required fields are marked *