Tuesday, June 23, 2015

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

Andrew (2015/06/23):  I posted this article on a blog many months ago, which has long since disappeared off the internet.  A few people found it useful at the time, so I decided to repost it here:

In Linux Makefiles, you will often see a notation which looks like this:
This is called "expansion assignment".
So what's the difference between expansion assignment and ordinary variable assignment (=)? Let's get straight into a coding example:
So nothing unexpected or unusual here! But lets try the same thing using expansion assignment ( := ):
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: