1. 程式人生 > >Makefile中=,:=,?= 的含義

Makefile中=,:=,?= 的含義

最近學習Makefile,複習一下以前學過的知識,參考<跟我一起學Makefile>中第七部分:使用變數

參考部落格:https://www.cnblogs.com/oracleloyal/p/6092526.html

1.Makefile中=,:=,?=的含義

"="

make會將整個makefile展開後,再決定變數的值。也就是說,變數的值將會是整個makefile中最後被指定的值

在“=”左側是變數,右側是變數的值,右側變數的值可以定義在檔案的任何一處,也就是說,右側中的變數不一定非要是已定義好的值,其也可以使用後面定義的值

":="

前面的變數不能使用後面的變數,只能使用前面已定義好了的變數

"?="

FOO ?= bar
其含義是,如果 FOO 沒有被定義過,那麼變數 FOO 的值就是“bar”,如果 FOO 先前被定義
過,那麼這條語將什麼也不做,其等價於:
ifeq ($(origin FOO), undefined)
FOO = bar
endif

2.小實驗

[email protected]:/lianxi/lianxi_c++/duotai# make test
###test code start!
###foo = HUH?
[email protected]:/lianxi/lianxi_c++/duotai# 
  1 .PHONEY:all clean test
  2 default:all
  3 
  4 #-----variables-----
  5 FILE:=*.out
  6 EMPTY:=
  7 
  8 foo=$(bar)
  9 bar=$(huh)
 10 huh=HUH?
 11 #-----clean trash-----
 12 clean:
 13     @echo "###start to clean trash!"
 14 ifeq ($(EMPTY), $(wildcard $(FILE)))
 15     @echo "###not exist *.out, do nothing!"
 16 else
 17     @echo "###exist *.out, delete it!"
 18     rm $(FILE)
 19 endif
 20 
 21 #-----compile-----
 22 all:clean
 23     @echo "###start to compile!"
 24     g++ ./virtual_inherit_sample.cpp -g -m32 -o b.out
 25     @echo "###compile finish!"
 26 
 27 #-----test-----
 28 test:
 29     @echo "###test code start!"
 30     @echo "###foo = $(foo)"
 31