Assign values to variables in Makefile and assign values to makefile
Variable assignment in Makefile
BEIJING-Haidian District
First, the Makefile syntax is as follows:
Targets: prerequisites
Command
.........
Or:
Targets: prerequisites; command
Command
.........
Targets is a file name separated by spaces. Wildcards can be used. In general, our goal is basically one file, but it may also be multiple files.
Command is a command line. If it is not in a line with "targetrerequisites", it must start with the [Tab key]. If it is in a line with prerequisites, you can use a semicolon to separate it.
Prerequisites is the object (or dependency target) on which the target depends ). If a file is newer than the target file, the target is regarded as "outdated" and is considered to need to be re-compiled.
Because makefile is related to the compilation rules of the entire project. There are many source files in a project. They are stored in several directories by type, function, and module. When we modify, add, or delete some source files, we need to modify the corresponding Makefile, if the source file is used in multiple places in Makefile, we need to modify multiple places in Makefile, which will cause a lot of inconvenience. In order to make makefile easier to maintain, variables can be used in makefile. The makefile variable is a string, which may be better understood as a macro in C language.
There are several ways to assign values to variables. The difference between "=" and ": =" is somewhat confusing ~
= Is a recursive expansion variable
Value1 = 1
Value2 = $ (value1)
Value1 = 2
In the end, $ (value2) becomes 2.
: = Is a directly expanded variable
Value1: = 1
Value2: = $ (value1)
Value1: = 2
Eventually $ (value2) is 1
? = Condition assignment
Value? = Abc indicates that if the value is not used before, the value is assigned abc. If the value is used before, the value is not assigned.
+ = Append Value
Value = filename1.o filename2.o
Value + = filename3.o
Then $ (value) is filename1.o filename2.o filename3.o
When assigning values to variables, if they cannot be placed in a row, they can be connected with the \ symbol.