Make and the ?= operator

The GNU crew improved Make with lots of handy stuff. One such gem that I found today is the ?= operator, which only defines a macro if it is not already defined (like as an environment variable). It behaves almost like wrapping the assignment in ifndef. John Gramm-Cunning has a good explanation of this subtle difference.

Here’s an example of ?= in action.

INSTALL_HOST ?= root@myhost.example.com

The INSTALL_HOST macro will have the value “root@myhost.example.com” unless someone specifies it as an environment variable when invoking make. This makes its value easily overridable by users calling your Makfile, like this:

$ INSTALL_HOST=root@foobar.com make install

I find it pretty handy.

However, I have one gripe. Make in general is tough to learn because its keywords and operators are not Googlable. Say I find the ?= operator in a Makefile and I want to look it up. Normally, I would just punch it into Google, but that just gives you a bunch of junk results. Not a huge deal, but it’s tough to find good docs out there for this reason

Has one comment to “Make and the ?= operator”

You can leave a reply or Trackback this post.
  1. Awesome! I didnt know about that! Thanks for writing about it!