Use variables of Makefile in linux Programming

Source: Internet
Author: User
The usage variable of Makefile in linux programming-Linux general technology-Linux technology and application information. For details, see the following. How variables work:
1. The variable is a text string;
2. When Makefile is executed, the variables are automatically displayed in the original mode where they are used;
3. variables can be used in: "target", "dependency target", "command", or other parts of Makefile;

Variable naming rules:
1. contains characters, numbers, and underscores (can begin with a number). The characters are case sensitive;
2. It should not contain ":", "#", "=", or null characters (blank space, carriage return, etc );
3. There are automation variables in Makefile, such as "$ <" and "$;

I. Basics of Variables
1. Initial values are given during Declaration;
2. Add "$" before the variable name during use ";
Note: It is best to include the variable with parentheses "()" or braces.
3. The $ character is represented by $.

Example 1:
Foo = c
Prog. o: prog. $ (foo)
$ (Foo)-$ (foo) prog. $ (foo)
After expansion:
Prog. o: prog. c
Cc-c prog. c


2. Variables in Variables

Variables can be defined in either of the following ways:

1. Variable 1 = $ (variable 2 );
Note:
1. Variable 2 can be defined anywhere in the file;
2. Variable 2 is not necessarily defined

Example 1:
Foo = $ (bar)
Bar = $ (ugh)
Ugh = Huh?
All:
Echo $ (foo)

Result: After "make all" is executed, the value of the variable $ (foo) is "Huh ?"

Note:
1. Advantage: the real value of the variable is defined later.
For example:
CFLAGS = $ (include_dirs)-O
Include_dirs =-Ifoo-Ibar
Result: CFLAGS =-Ifoo-Ibar-O is displayed.

2. disadvantage: This form can cause recursive definitions.
For example:
CFLAGS = $ (CFLAGS)-O or:
A = $ (B)
B = $ ()
Result: make falls into an infinite variable expansion process. However, make can detect such definitions and report errors;

3. When using functions in variables, make runs very slowly. make calls its own functions "wildcard" and "shell" and an unpredictable error occurs.
This is a 2nd way to define variables.

2. Variable 1: = [$ (variable 2)] variable value

Example 1:
X: = foo
Y: = $ (x) bar
X: = later

Result: The execution is equivalent:
Y: = foo bar
X: = later

Note:
1. Advantage: The preceding variables cannot use the following variables. They can only use the previously defined variables.
For example:
Y: = $ (x) bar
X: = foo
Result: y = bar;

Example 2: make functions, conditional expressions, and the use of a system variable "MAKELEVEL"
Ifeq (0, $ {MAKELEVEL })
Cur-dir: = $ (shell pwd)
Whoami: =$ (shell whoami)
Host-type: = $ (shell arch)
MAKE: =$ {MAKE} host-type =$ {host-type} whoami =$ {whoami}
Endif

Note:
1. The system variable MAKELEVEL records the number of calls to the current Makefile.

Example 3: define a variable whose value is a space
Nullstring: =
Space: = $ (nullstring) # end of the line

Note:
1. nullstring is an Empty variable. An Empty variable is used to indicate the start of the variable value.
2. Use the "#" annotator to indicate that the variable definition is terminated. In this way, the variable whose value is a space is defined.

Example 4:
Dir: =/foo/bar # directory to put the frobs in
Note:
1. the dir value is "/foo/bar", followed by four spaces;
2. $ (dir)/file =/foo/bar/file. This is incorrect.
3. The special operator is "? ="
Example 1:
FOO? = Bar

Note:
1. If FOO has not been defined, the FOO value of the variable is bar ";
2. If FOO has been previously defined, nothing will be done in this sentence;
3. The statement is equivalent:
Ifeq ($ (origin FOO), undefined)
FOO = bar
Endif

Iii. Advanced variable usage

Advanced usage of the two variables

1. Replace variable values

Overview: replace the common part of a variable with other parts.
Syntax: "$ (var: a = B)" or "$ {var: a = B }"
Explanation: replace all the "a" in the variable "var" with the "B" string ending with ".
Note: "end" means "space" or "Terminator ".

Example 1:
Foo: = a. o B. o c. o
Bar: = $ (foo:. o =. c)

Note:
1. The first line defines a variable "$ (foo;
2. In the second line, replace all the ". o" strings in "$ (foo)" with ". c ";
3. The final result is: $ (bar) = "a. c B. c. c ";
4. Another variable replacement technology "static mode ":
Foo: = a. o B. o c. o
Bar: = $ (foo: %. o = %. c)
Note: The replaced string has the same mode. The above foo must contain a "%" (any character) character. In this example, the value of the $ (bar) variable is also ". c B. c. c ".

2. Treat the value of a variable as a variable.
Example 1
X = y
Y = z
A: =$ ($ (x ))
Note:
1. The value of $ (x) is "y ";
2. $ (x) is $ (y );
3. The value of $ (a) is "z ";

Example 2:
X = $ (y)
Y = z
Z = Hello
A: =
Note:
0. $ (a) = $ (x ))
1. $ (x) = $ (y)
2. $ (x) = $ (y ))
3. $ (y) = $ (z)
4. $ (z) = hello
5. $ (a) = hello

Example 3:
X = variable1
Variable2: = Hello
Y = $ (subst 1, 2, $ (x ))
Z = y
A: =$ ($ (z )))
Note:
1. (subst str1, str2, varb) replace str1 in varb with str2
2. y = variable2
3. a: =$ ($ (z )))
4. $ (z) = y
5. a: =$ ($ (y ))
6. $ (y) = variable2
7. a: = $ (variable2)
8. a: = "Hello"

Example 4: multiple variables are used to form the name of a variable, and then their values are obtained.
First_second = Hello
A = first
B = second
All = $ ($ a _ $ B)
Note:
1. "$ a _ $ B" forms "first_second ";
2. The value of $ (all) is "Hello ".

Example 5:
A_objects: = a. o B. o c. o
Export objects: = 1.o 2.o 3.o
Sources: = $ (a1) _ objects:. o =. c)

Note:
1. If $ (a1) is "a", then $ (sources) is "a. c B. c ";
2. If $ (a1) is "1", then $ (sources) is "1.c 2.c 3. c"

Example 6: replace variables. Functions and conditional statements are used together.
Ifdef do_sort
Func: = sort
Else
Func: = strip
Endif
Bar: = a d B g q c
Foo: = $ (func) $ (bar ))
Note:
1. if "do_sort" is defined, foo: = $ (sort a d B g q c), so the value of $ (foo) is "a B c d g q ";
2. If "do_sort" is not defined, foo: = $ (strip a d B g q c)
3.

Example 6: Apply the "variable value as a variable" technology to the left of the operator
Dir = foo
$ (Dir) _ sources: = $ (wildcard $ (dir)/*. c)
Define $ (dir) _ print
Lpr $ (dir) _ sources)
Endef
Note:
1. This example defines three variables: "dir", "foo_sources", and "foo_print ".

Iv. append variable values
Append variable: use the "+ =" Operator

Example 1:
Objects = main. o foo. o bar. o utils. o
Objects + = another. o
Note:
1. $ (objects) = "main. o foo. o bar. o utils. o another. o"
2. It can also be used as below;
Objects = main. o foo. o bar. o utils. o
Objects: = $ (objects) another. o
3. "+ =" relative ": =" more concise;
4. If the variable has not been defined before, "+ =" will automatically become "= ";
5. If there is a variable definition before the variable, "+ =" will inherit from the value assignment of the previous operation.
6. If the previous value is ": =", "+ =" will use ": =" as its value assignment, for example:
Variable: = value
Variable + = more
It is equivalent:
Variable: = value
Variable: = $ (variable) more
7. If the previous value is "=", "+ =" uses "=" as its value assignment, for example:
Variable = value
Variable + = more
It is equivalent:
Variable = value
Variable = $ (variable) more (note)
Note: make automatically solves the above recursive definition problem.

V. override indicator
Make can set the variable through the command line parameter. The value assigned to this variable is ignored in Makefile.
If you want to set this type of parameter value in Makefile, you can use the "override" indicator.

Syntax:
Override =
Override : =
Override + =

Define multiline variables and use the define indicator;
You can use the ovveride indicator before the define indicator.

For example:
Override define foo
Bar
Endef

6. multiline Variables
You can use the define keyword to set variable values with line breaks, which is conducive to defining a series of commands (the "command package" technology mentioned above is to use this keyword ).

Note:
1. The define indicator is followed by the variable name;
2. Repeat a row as the variable value;
3. The definition ends with the keyword endef;
4. It works in the same way as the = Operator;
5. The value of a variable can contain functions, commands, text, or other variables;
6. The command variables defined by define must start with the [Tab] key; otherwise, make will not regard them as commands.

Example 1:
Define two-lines
Echo foo
Echo $ (bar)
Endef

VII. Environment Variables
Note:
1. The priority of system environment variables defined by the same variable in the system is lower than that defined in Makefile.
2. make uses the "-e" parameter to replace the variables in the Makefile with the same variables in the system.
3. During the make nested call, the variables defined by make in the upper Makefile are transmitted to the sub-Makefile as system variables.
4. During the make nested call, variables defined in the upper Makefile are transmitted to the sub-Makefile using export.
5. Note that variable definition in the system environment may cause some trouble.

8. target variables
Target variable definition:
The function scope is a rule and associated rules. The value is valid only within the rule and associated rules, and the variable that does not affect global variables of the same name outside the rule chain is called the target variable.

Global variable definition:
The variables defined in Makefile are called "global variables" and are applied to the entire file.

Automated variable definition:
The value of a variable depends on the rule's target and the variable defined by the target. For example, "$ <"

Note:
1. The Target local Variable is called "Target-specific Variable". It can have the same name as the global Variable;
2. changes do not affect global variables with the same name;

Syntax format of target variable definition:
:
: Overide

Syntax format description;
1. It can be a variety of assignment expressions, such as "=", ": =", "+ =", or "? = ";
2. The second syntax is for the variables or system environment variables brought in by the make command line;

Example 1:
Prog: CFLAGS =-g
Prog: prog. o foo. o bar. o
$ (CC) $ (CFLAGS) prog. o foo. o bar. o
Prog. o: prog. c
$ (CC) $ (CFLAGS) prog. c
Foo. o: foo. c
$ (CC) $ (CFLAGS) foo. c
Bar. o: bar. c
$ (CC) $ (CFLAGS) bar. c

Note:
1. no matter what the global $ (CFLAGS) value is, among the prog target and all the rules it raises (prog. o foo. o bar. o rules), $ (CFLAGS) values are "-g"

9. Mode Variables

Pattern variable definition: defines variables on all targets that conform to this pattern.
Pattern-specific Variable

Example 1: define the target variable for all targets ending with [. o:
%. O: CFLAGS =-O

Note:
1. The "Mode" of make generally contains at least one "% ".
Syntax:
:
: Override
Note:
Override is also for variables passed in the system environment or variables specified by the make command line.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.