Overwrite variable from makefile

前端 未结 2 1336
野的像风
野的像风 2021-01-12 06:10

In my makefile I have a variable FOO:

FOO = /path/to/bar

Is it possible to overwrite this variable during the makefile call? S

相关标签:
2条回答
  • 2021-01-12 06:27

    Specify them as Var=Value before you specify the target, like make FOO=/path/to/foo all.

    $ cat Makefile 
    Foo = asdf
    
    all:
        echo $(Foo)
    
    $ make all
    echo asdf
    asdf
    $ make Foo=bar all
    echo bar
    bar
    
    0 讨论(0)
  • 2021-01-12 06:33

    The ways that variables get assigned values is specified in the How Variables Get Their Values section of the GNU make Manual.

    Variables can get values in several different ways:

    • You can specify an overriding value when you run make. See Overriding Variables.
    • You can specify a value in the makefile, either with an assignment (see Setting Variables) or with a verbatim definition (see Defining Multi-Line Variables).
    • Variables in the environment become make variables. See Variables from the Environment.
    • Several automatic variables are given new values for each rule. Each of these has a single conventional use. See Automatic Variables.
    • Several variables have constant initial values. See Variables Used by Implicit Rules.

    So, as Colonel Thirty Two indicates, you can override variables set in the makefile on the command line.

    You can also, if you expect this to be a variable that you want to set persistently, use the ?= assignment and then environment values for that variable will be used.

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