Conditional statement


1. Conditional statements are supported in makefiles

  • Make execution can be determined based on the value of the condition
  • You can compare two different variables or the value of a variable and a constant
ifxxx (arg1,arg2)
#do true
else
#do false
#endif
Copy the code

Note: Conditional statements can only be used to control statements that make actually executes; However, you cannot control the execution of commands in rules.


2. Conditional statement syntax description:

Common form ifxxx (arg1,arg2) other legal forms

Ifxxx "arg1" "arg2" ifxxx "arg1" "arg2" ifxxx "arg1" "arg2"Copy the code

Conditional judgment syntax format:

3. Condition judgment keywords

Keyword Function

.PHONY : test

var1 := A
var2 := $(var1)
var3 := 

test :
    ifeq ($(var1),$(var2))
    @echo "var1 == var2"
    else
    @echo "var1 ! = var2"
    endif
    
    ifneq ($(var3), )
    @echo "var3 is not empty"
    else
    @echo "var3 is empty"
    endif
    
    ifdef var1
    @echo "var1 is not empty"
    else
    @echo "var1 is empty"
    endif
    
    ifndef var3
    @echo "var3 is empty"
    else
    @echo "var3 is not empty"
    endif
Copy the code


Some engineering experience

  • Conditional statements can be preceded by Spaces, but not by Tab characters (‘ \t ‘)
  • Do not use automatic variables in conditional statements (@,@,^,$<)
  • A complete conditional statement must be in the same Makefile
  • Conditional judgment is similar to macro in C language, which is effective in the pre-processing stage and invalid in the execution stage
  • Make first evaluates the expression when loading the makefile. It determines what to execute based on the expression of the statement

Summary:

  • Condition determination Determines make execution based on the value of the condition
  • A conditional judgment can compare two different variables or the value of a variable and a constant
  • Condition judgment is effective in the pre-processing stage, but not in the execution stage
  • Conditions cannot control the execution of commands in rules