How to print out a variable in makefile

后端 未结 15 1058
臣服心动
臣服心动 2020-12-07 06:51

In my makefile, I have a variable \'NDK_PROJECT_PATH\', my question is how can I print it out when it compiles?

I read Make file echo displaying "$PATH" st

相关标签:
15条回答
  • 2020-12-07 07:36

    This makefile will generate the 'missing separator' error message:

    all
        @echo NDK_PROJECT_PATH=$(NDK_PROJECT_PATH)
    
    done:
            @echo "All done"
    

    There's a tab before the @echo "All done" (though the done: rule and action are largely superfluous), but not before the @echo PATH=$(PATH).

    The trouble is that the line starting all should either have a colon : or an equals = to indicate that it is a target line or a macro line, and it has neither, so the separator is missing.

    The action that echoes the value of a variable must be associated with a target, possibly a dummy or PHONEY target. And that target line must have a colon on it. If you add a : after all in the example makefile and replace the leading blanks on the next line by a tab, it will work sanely.

    You probably have an analogous problem near line 102 in the original makefile. If you showed 5 non-blank, non-comment lines before the echo operations that are failing, it would probably be possible to finish the diagnosis. However, since the question was asked in May 2013, it is unlikely that the broken makefile is still available now (August 2014), so this answer can't be validated formally. It can only be used to illustrate a plausible way in which the problem occurred.

    0 讨论(0)
  • 2020-12-07 07:39

    If you simply want some output, you want to use $(info) by itself. You can do that anywhere in a Makefile, and it will show when that line is evaluated:

    $(info VAR="$(VAR)")
    

    Will output VAR="<value of VAR>" whenever make processes that line. This behavior is very position dependent, so you must make sure that the $(info) expansion happens AFTER everything that could modify $(VAR) has already happened!

    A more generic option is to create a special rule for printing the value of a variable. Generally speaking, rules are executed after variables are assigned, so this will show you the value that is actually being used. (Though, it is possible for a rule to change a variable.) Good formatting will help clarify what a variable is set to, and the $(flavor) function will tell you what kind of a variable something is. So in this rule:

    print-% : ; $(info $* is a $(flavor $*) variable set to [$($*)]) @true
    
    • $* expands to the stem that the % pattern matched in the rule.
    • $($*) expands to the value of the variable whose name is given by by $*.
    • The [ and ] clearly delineate the variable expansion. You could also use " and " or similar.
    • $(flavor $*) tells you what kind of variable it is. NOTE: $(flavor) takes a variable name, and not its expansion. So if you say make print-LDFLAGS, you get $(flavor LDFLAGS), which is what you want.
    • $(info text) provides output. Make prints text on its stdout as a side-effect of the expansion. The expansion of $(info) though is empty. You can think of it like @echo, but importantly it doesn't use the shell, so you don't have to worry about shell quoting rules.
    • @true is there just to provide a command for the rule. Without that, make will also output print-blah is up to date. I feel @true makes it more clear that it's meant to be a no-op.

    Running it, you get

    $ make print-LDFLAGS
    LDFLAGS is a recursive variable set to [-L/Users/...]
    
    0 讨论(0)
  • 2020-12-07 07:39

    All versions of make require that command lines be indented with a TAB (not space) as the first character in the line. If you showed us the entire rule instead of just the two lines in question we could give a clearer answer, but it should be something like:

    myTarget: myDependencies
            @echo hi
    

    where the first character in the second line must be TAB.

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