What does CC?= in a Makefile mean?

inquam picture inquam · Jun 3, 2010 · Viewed 38.4k times · Source

I have a Makefile for a C program that has the declaration

CC?=gcc

Changing it to

CC?=g++

does NOT make it compile with g++. Changing it to

CC=g++

DOES make it use g++.

So I wonder what the ?= operator does? My guess is that it looks at a environment variable to decide which compiler to use and if it's not set then use gcc? Anyone who can clear this up?

Answer

kennytm picture kennytm · Jun 3, 2010

From http://www.gnu.org/software/make/manual/make.html:

There is another assignment operator for variables, `?='. This is called a conditional variable assignment operator, because it only has an effect if the variable is not yet defined. This statement:

 FOO ?= bar

is exactly equivalent to this (see The origin Function):

 ifeq ($(origin FOO), undefined)
   FOO = bar
 endif

Probably CC is already defined as gcc, so CC ?= g++ won't override the existing gcc.