简单聊一下makefile中的 =, :=, ?=和+=

   最容易混淆的是 =和:=, 我们先来看看makefile:

[plain] view plain copy
  1. x = hello  
  2. y = $(x)  
  3. x = world  
  4. test1: test1.cpp  
  5.     @echo $(y)  
       执行make后, 结果为world,  再看改动后的makefile:

[plain] view plain copy
  1. x = hello  
  2. y := $(x)  
  3. x = world  
  4. test1: test1.cpp  
  5.     @echo $(y)  
       执行make后, 结果为hello,  为什么有区别呢?

       因为=其实类似于C++中的引用, 相当于y和x绑定了, 所以在echo的时候, 值为world

       而:=其实类似于C++中的赋值, 所以在echo的时候, 值为hello.   在makefile中, 强烈建议使用:=进行复制。


       y?=x是什么呢? 其实很好理解, 它的意思是: 如果y没有赋值过, 那就赋值。 如果赋值过, 那本次就不赋值了。

       y+=x是什么呢?  很好理解, 其实就是C++中的+=, 相当于累加。


       这些东西本来很简单, 但我发现, 有很多地方没讲清楚, 所以来唠叨一下。


猜你喜欢

转载自blog.csdn.net/qq_21435127/article/details/80635405