Makefile If-Then Else and Loops

前端 未结 4 1530
青春惊慌失措
青春惊慌失措 2020-12-24 10:18

Can someone explain how to use if-then statements and for loops in Makefiles? I can\'t seem to find any good documentation with examples.

相关标签:
4条回答
  • 2020-12-24 11:01

    You do see for loops alot of the time, but they are usually not needed. Here is an example of how one might perform a for loop without resorting to the shell

    LIST_OF_THINGS_TO_DO = do_this do_that 
    $(LIST_OF_THINGS_TO_DO): 
           run $@ > $@.out
    
    SUBDIRS = snafu fubar
    $(SUBDIRS):
         cd $@ && $(MAKE)
    
    0 讨论(0)
  • 2020-12-24 11:09

    Have you tried the GNU make documentation? It has a whole section about conditionals with examples.

    0 讨论(0)
  • 2020-12-24 11:10

    Here's an example if:

    ifeq ($(strip $(OS)),Linux)
            PYTHON = /usr/bin/python
            FIND = /usr/bin/find
    endif
    

    Note that this comes with a word of warning that different versions of Make have slightly different syntax, none of which seems to be documented very well.

    0 讨论(0)
  • 2020-12-24 11:14

    Conditional Forms

    Simple

    conditional-directive
    text-if-true
    endif
    

    Moderately Complex

    conditional-directive
    text-if-true
    else
    text-if-false
    endif
    

    More Complex

    conditional-directive
    text-if-one-is-true
    else
    conditional-directive
    text-if-true
    else
    text-if-false
    endif
    endif
    

    Conditional Directives

    If Equal Syntax

    ifeq (arg1, arg2)
    ifeq 'arg1' 'arg2'
    ifeq "arg1" "arg2"
    ifeq "arg1" 'arg2'
    ifeq 'arg1' "arg2"
    

    If Not Equal Syntax

    ifneq (arg1, arg2)
    ifneq 'arg1' 'arg2'
    ifneq "arg1" "arg2"
    ifneq "arg1" 'arg2'
    ifneq 'arg1' "arg2"
    

    If Defined Syntax

    ifdef variable-name
    

    If Not Defined Syntax

    ifndef variable-name  
    

    foreach Function

    foreach Function Syntax

    $(foreach var, list, text)  
    

    foreach Semantics
    For each whitespace separated word in "list", the variable named by "var" is set to that word and text is executed.

    0 讨论(0)
提交回复
热议问题