Deprecated: Assigning the return value of new by reference is deprecated in /usr/share/php/HTML/Menu.php on line 228

Strict Standards: Declaration of HTML_Menu3::_findNodeType() should be compatible with HTML_Menu::_findNodeType($nodeId, &$nodeUrl, $level) in /var/www/wi-fizzle.com/lib/HTML/Menu3.php on line 49
Wi-Fizzle.com | What does the colon-equal sign ( := ) mean in a Makefile?
Wi-Fizzle.com - Putting the fizzle in Wi-Fi since 2005 .. (yes, this was a poor choice for a domain name)

<div class="news_item">#474<div class="news_title">What does the colon-equal sign ( := ) mean in a Makefile?

Posted by dandriff on Tuesday August 18, 2015@01:37PM

What is the colon-equals sign ( := ) in Makefiles?

In Linux makefiles, you will often see a notation which looks like this:

MyNumber := 123456789

This is called "expansion assignment".

So what is the difference between expansion assignment and ordinary variable assignment ( = )?

Let's get straight into a coding example:

    ANIMAL = FROG
    VAR = "$(ANIMAL) DOG CAT"
    ANIMAL = PORCUPINE
    
    test:
        @echo $(VAR)

Output:

    PORCUPINE DOG CAT

Nothing unexpected or unusual here.

Lets try the same thing using expansion assignment ( := ):

    ANIMAL = FROG
    VAR := "$(ANIMAL) DOG CAT"
    ANIMAL = PORCUPINE
    
    test:
        @echo $(VAR)

Output:

    FROG DOG CAT

Oh no! It appears that we have a frog instead of the porcupine we wanted. So what happened?
When using the equals sign the variable is only expanded when used; in other words: when we run test.

In the second example we used ":=". This means that the variable is expanded at the time of assignment. If we want to see PORCUPINE again in the second example, we need to make a slight modification to it:

    ANIMAL = FROG
    VAR := "$(ANIMAL) DOG CAT"
    ANIMAL = PORCUPINE
    VAR := "$(ANIMAL) DOG CAT"
    
    test:
        @echo $(VAR)

Output:

    PORCUPINE DOG CAT

So there you have it! Now you are equipped to know the difference between equals and colon-equals in Makefiles.