Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does CC?= in a Makefile mean?

Tags:

gcc

makefile

g++

cc

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?

like image 548
inquam Avatar asked Jun 03 '10 12:06

inquam


People also ask

What does ?= Mean in makefile?

?= indicates to set the KDIR variable only if it's not set/doesn't have a value. For example: KDIR ?= "foo" KDIR ?= "bar" test: echo $(KDIR)

What is CC O in makefile?

c=.o} means, take the variable value ${SRCS} , which is a string composed of words separated by spaces, and for each word, replace the suffix .

What does %O %c mean in makefile?

The simple answer is that %.o is a target that matches any file ending in .o. "%.o: %. c" means that any file ending in .o depends on the same filename ending in . c to be present.

What is CC variable?

This is a CMake Environment Variable. Its initial value is taken from the calling process environment. Preferred executable for compiling C language files. Will only be used by CMake on the first configuration to determine C compiler, after which the value for CC is stored in the cache as CMAKE_C_COMPILER .


2 Answers

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.

like image 107
kennytm Avatar answered Sep 25 '22 13:09

kennytm


The ?= operator sets the variable only if it isn't already set: info make* Using Variables* Setting.

like image 28
Marcelo Cantos Avatar answered Sep 23 '22 13:09

Marcelo Cantos