I wrote together with Makefile (seven)

Disclaimer: This article is CSDN blogger original article "haoel", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/haoel/article/details/2892

 

---- use variables
  defined in the Makefile variables, like C / C ++ macro language, he represents a text string, when executed in the Makefile in the original model as it automatically deployed in the use The place. With C / C ++ The difference is, you can change its value in the Makefile. In the Makefile, variables can be used in other parts of the "target", "target-dependent", "command" or the Makefile.
  Ordered the variable names can contain characters, numbers, underscores (can start with a number), but should not contain ":", "#", "=" or empty characters (spaces, carriage returns, etc.). Variables are case-sensitive, "foo", "Foo" and "FOO" are three different variable names. Traditional Makefile variable name is all uppercase naming, but I recommend using the variable name with the case, such as: MakeFlags. And system variables to avoid conflicts, and the accident happened.
  There are some very strange string variable, such as "$ <", "$ @" and so on, these variables are automated, I will explain later.

Basics 1. Variables

  Declare variables need to be given when the initial value, while in use, the need to add "$" symbol before the variable name, but preferably with parentheses "()" or braces "{}" to the variable comprising up. If you want to use a real "$" character, then you need to be represented by "$$."

  Variables can be used in many places, such as the rule of the "target," "dependence", "order" and the new variable. Look at an example:

objects = program.o foo.o utils.o

program : $(objects)
    cc -o program $(objects)

$(objects) : defs.h

  Variables will unfold exactly where to use it, just like C / C ++ macros, like, for example:

foo = c

prog.o : prog.$(foo)
    $(foo)$(foo) -$(foo) prog.$(foo)

  Expanded get:

prog.o: prog.c 
    cc c prog.c

  Of course, do not be so dry in your Makefile in here is just an example to show the real appearance of the Makefile variable unfolded at points of use. It shows is a "substitution" principle.

  In addition, to the variable parentheses entirely for safer use this variable, in the example above, if you do not want to add a variable in parentheses, it can, but I strongly recommend you to add a variable in parentheses.

 

2. The variables variables

  When defining the value of a variable, we can use the value of the variable to construct other variables, there are two ways to use variable value definitions of the variables in the Makefile.

  Look at the first way, which is simple to use the "=", the "=" is a variable on the left side, the right side is the value of the variable, the value of the right side of the variable can be defined in any one document, i.e. right variables are not necessarily values ​​have been defined, the value of which may be used as defined below. Such as:

foo = $(bar)
bar = $(ugh)
ugh = Huh?

all:
    echo $(foo)

  We execute "make all" will play the variable $ (foo) value is "Huh?" (Value of $ (foo) is $ (bar), the value of $ (bar) is $ (ugh), $ (ugh) the value is "Huh?") visible, you can use the back of the variables are variables defined.

  This feature is a good place, there are bad places, a good place, we can put the true value of the variable pushed back to the definition, such as:

CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar

  When "CFLAGS" is expanded in the command, it would be "-Ifoo -Ibar -O". However, this form there are bad places, and that is a recursive definition, such as:

CFLAGS = $(CFLAGS) -O
或:
A = $(B)
B = $(A)

  It will make an infinite variables of the process to unfold, of course, we make have the ability to detect such a definition, and will complain. There is if you use the function in a variable, then, this way we will make a very slow operation, the worse is that he will make use of a function of two "wildcard" and "shell" unpredictable errors occur . Because you do not know these two functions can be called many times.

  To avoid this the above method, we can define another way to make a variable with the variables used. This method uses the ": =" operator,

Such as:

x := foo
y := $(x) bar
x := later

Which is equivalent to:

y := foo bar
x := later

  It is worth mentioning that this method can not be used in front of the variables behind the variables, only variables previously defined the good. If this is the case:

y := $(x) bar
x := foo

  So, the value of y is "bar", instead of "foo bar".

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zhuangquan/p/11883665.html