How to print out a variable in makefile

后端 未结 15 1057
臣服心动
臣服心动 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:24

    Run make -n; it shows you the value of the variable..

    Makefile...

    all:
            @echo $(NDK_PROJECT_PATH)
    

    Command:

    export NDK_PROJECT_PATH=/opt/ndk/project
    make -n 
    

    Output:

    echo /opt/ndk/project
    
    0 讨论(0)
  • 2020-12-07 07:27

    from a "Mr. Make post" https://www.cmcrossroads.com/article/printing-value-makefile-variable

    Add the following rule to your Makefile:

    print-%  : ; @echo $* = $($*)
    

    Then, if you want to find out the value of a makefile variable, just:

    make print-VARIABLE
    

    and it will return:

    VARIABLE = the_value_of_the_variable
    
    0 讨论(0)
  • 2020-12-07 07:27

    No need to modify the Makefile.

    $ cat printvars.mak
    print-%:
            @echo '$*=$($*)'
    
    $ cd /to/Makefile/dir
    $ make -f ~/printvars.mak -f Makefile print-VARIABLE
    
    0 讨论(0)
  • 2020-12-07 07:29

    @echo $(NDK_PROJECT_PATH) is the good way to do it. I don't think the error comes from there. Generally this error appears when you mistyped the intendation : I think you have spaces where you should have a tab.

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

    This can be done in a generic way and can be very useful when debugging a complex makefile. Following the same technique as described in another answer, you can insert the following into any makefile:

    # if the first command line argument is "print"
    ifeq ($(firstword $(MAKECMDGOALS)),print)
    
      # take the rest of the arguments as variable names
      VAR_NAMES := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
    
      # turn them into do-nothing targets
      $(eval $(VAR_NAMES):;@:))
    
      # then print them
      .PHONY: print
      print:
              @$(foreach var,$(VAR_NAMES),\
                echo '$(var) = $($(var))';)
    endif
    

    Then you can just do "make print" to dump the value of any variable:

    $ make print CXXFLAGS
    CXXFLAGS = -g -Wall
    
    0 讨论(0)
  • 2020-12-07 07:35

    I usually echo with an error if I wanted to see the variable value.(Only if you wanted to see the value. It will stop execution.)

    @echo $(error NDK_PROJECT_PATH= $(NDK_PROJECT_PATH))

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