Search code examples
makefiletarget

How using variable of target in my makefile?


I write the target in my makefile when I follow the example of Linux develop C book. Below is a cut down of the my makefile:

result:=


all :  
$(result) = $(subst a, A, how are you)

        echo -n "the result is :"
        echo $(result)


.PHONY: all

In shell,

it@ubuntu:~/luke/c_test$ make -s all
makefile:5: *** empty variable name.  Stop.

How could I return the value of function to "result" in target?


Solution

  • Your first assignment is correct. Your second assignment erroneously uses variable interpolation syntax. It's variable := value not $(variable) := value; the latter would try to use the value of variable as the name of the variable to assign the value to, but in your case, it's empty.

    Assigning the variable inside a recipe is also wrong; the stuff inside a recipe should be tab-indented shell commands.

    result := $(subst a, A, how are you)
    
    all:
        echo "the result is: $(result)"
    
    .PHONY: all
    

    or maybe even inline the function call:

        echo "the result is: $(subst a,A,how are you)"
    

    It's not completely impossible to assign a variable only for the duration of a single recipe, but I guess you are still trying to learn the basic syntax.

    Maybe also notice the difference between := and =. The former should be preferred in GNU Make unless you specifically want make to evaluate the assignment every time the value is expanded.