The condition judgment of Linux makefile Tutorial six [turn]

Source: Internet
Author: User

Judging using conditions

——————

By using conditional judgment, you can have make to select different execution branches depending on the runtime's circumstances. A conditional expression can be a value of a comparison variable, or a value that compares variables and constants.

One, example

The following example determines whether the $ (CC) variable is "gcc" and, if so, compiles the target using the GNU function.

LIBS_FOR_GCC =-lgnu

Normal_libs =

Foo: $ (objects)

Ifeq ($ (CC), GCC)

$ (CC)-O-foo $ (objects) $ (LIBS_FOR_GCC)

Else

$ (CC)-O-foo $ (objects) $ (normal_libs)

endif

It can be seen that in this rule of the example above, the target "foo" could choose a different library of functions to compile the program based on the variable "$ (CC)" value.

We can see three keywords from the example above: ifeq, Else, and endif. Ifeq means the beginning of a conditional statement, and specifies a conditional expression that contains two arguments, separated by commas, with expressions enclosed in parentheses. else indicates that the conditional expression is false. ENDIF represents the end of a conditional statement, and any one of the conditional expressions should end with endif.

When our variable $ (CC) value is "gcc", the rule for the target Foo is:

Foo: $ (objects)

$ (CC)-O-foo $ (objects) $ (LIBS_FOR_GCC)

When our variable $ (cc) value is not "gcc" (e.g. "CC"), the rule for the target Foo is:

Foo: $ (objects)

$ (CC)-O-foo $ (objects) $ (normal_libs)

Of course, we can also write the above example more concise:

LIBS_FOR_GCC =-lgnu

Normal_libs =

Ifeq ($ (CC), GCC)

libs=$ (LIBS_FOR_GCC)

Else

libs=$ (Normal_libs)

endif

Foo: $ (objects)

$ (CC)-O-foo $ (objects) $ (LIBS)

Second, the grammar

The syntax for a conditional expression is:

<conditional-directive>

<text-if-true>

endif

And:

<conditional-directive>

<text-if-true>

Else

<text-if-false>

endif

where <conditional-directive> represents a conditional keyword, such as "ifeq". There are four of these keywords.

The first one is the "ifeq" we've seen before.

Ifeq (<ARG1>, <arg2>)

Ifeq ' <arg1> ' <arg2> '

Ifeq "<arg1>" "<arg2>"

Ifeq "<arg1>" ' <arg2> '

Ifeq ' <arg1> ' "<arg2>"

Compare the values of the parameter "Arg1" and "arg2". Of course, we can also use the Make function in the parameters. Such as:

Ifeq ($ (Strip $ (foo)),)

<text-if-empty>

endif

In this example, the "strip" function is used, and if the return value of this function is null (empty), then <text-if-empty> takes effect.

The second conditional keyword is "ifneq". The syntax is:

Ifneq (<ARG1>, <arg2>)

Ifneq ' <arg1> ' <arg2> '

Ifneq "<arg1>" "<arg2>"

Ifneq "<arg1>" ' <arg2> '

Ifneq ' <arg1> ' "<arg2>"

Whether the values for the comparison parameters "Arg1" and "arg2" are the same, or true if they are different. Similar to "ifeq".

The third conditional 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 just 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 conditional keyword is "ifndef". Its syntax is:

Ifndef <variable-name>

I will not say more, and "ifdef" is the opposite meaning.

In <conditional-directive> this line, the extra space is allowed, but cannot start with the [Tab] key (otherwise it is considered a command). The comment "#" is also safe. "Else" and "endif" are the same, as long as you do not start with the [Tab] key.

It is especially important to note that make is the value of the conditional expression when reading makefile, and select the statement based on the value of the conditional expression, so you'd better not put the automation variables (such as "[email protected]", etc.) into the conditional expression, Because automation variables are only at run time.

Also, to avoid confusion, make does not allow the entire conditional statement to be divided into two parts in separate files.

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.