Makefile Detailed condition judgment 20Use conditions to judge
——————
Using conditional judgment, you can allow make to choose a different branch of execution based on the different circumstances of the runtime. A conditional expression can be a value of a comparison variable, or a value that compares variables and constants.
1, example
The following example determines whether the $ (CC) variable is "gcc" and, if so, uses the GNU function to compile the target.
Visible, in this rule of the example above, target "foo" can compile the program by selecting a different function library based on the variable "$ (CC)" value.
We can see three keywords in the example above: ifeq, Else, and endif. The meaning of the ifeq represents the beginning of a conditional statement and specifies a conditional expression that contains two arguments, separated by commas, and the expression is enclosed in parentheses. else indicates a condition where the conditional expression is false. ENDIF represents the end of a conditional statement, and any conditional expression should end with a endif.
When our variable $ (CC) value is "gcc", the rule for the target Foo is:
The values for the comparison parameter "Arg1" and "arg2" are the same and, if they are different, true. Similar to "ifeq".
The third condition keyword is "ifdef". The syntax is:
Ifdef <variable-name>;
If the value of the variable <variable-name>; is not empty, then the expression is true. Otherwise, the expression is false. Of course,<variable-name>; can also be the return value of a function. Note that ifdef simply tests whether a variable has a value and does not extend the variable to its current position. Let's take a look at two examples:
Example one:
Bar =
Foo = $ (bar)
ifdef foo
Frobozz = yes
Else
Frobozz = No
endif
Example two:
Foo =
ifdef foo
Frobozz = yes
Else
Frobozz = No
endif
In the first example, the "$ (frobozz)" value is "yes" and the second is "no".
The fourth condition keyword is "ifndef". Its syntax is:
Ifndef <variable-name>;
This I will not say much, and "ifdef" is the opposite meaning.
In the <conditional-directive>; line, extra spaces are allowed, but you cannot start with the [Tab] key (otherwise it is considered a command). The annotation character "#" is also safe. "Else" and "endif" are the same, as long as they are not started with the [Tab] key.
Of particular note is that make is to evaluate the value of the conditional expression while reading the Makefile, and select the statement based on the value of the conditional expression, so you should not put automation variables (such as "$@", etc.) into the conditional expression, because the automation variable is at run time.
Also, to avoid confusion, make does not allow the entire conditional statement to be divided into two parts in a different file.
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.