Lerp
Lerp

Reputation: 3127

Defining Variables inside macro

How do you define a variable inside of a GNU make macro? I am using GNU Make 4.0 and whenever I do an assignment the variable is empty:

define TEST_MACRO
    $(info $(1))
    test_var := $(1)
    $(info $(test_var))
endef

$(call TEST_MACRO,test)

Outputs:

test
(blank line)

I have tried using recursive expansion for the assignment but I get the same results.

Upvotes: 1

Views: 680

Answers (2)

Lerp
Lerp

Reputation: 3127

I eventually went with something like this as I felt the double $ was messy.

define TEST_MACRO
   $(info B $(test_var))
endef

# Note test_var was defined after TEST_MACRO
test_var := test

$(eval $(value TEST_MACRO))

This has the disadvantage that you can't set the $1..$n variables but it's easier to read.


Edit - A better example of defining a variable inside of the macro. The above example was demonstrating how you would pass values into the macro, like you would with call

define TEST_MACRO
    test_var := test

    $(info B $(test_var))
endef

Upvotes: 0

Beta
Beta

Reputation: 99094

You haven't mentioned which version of Make you're using, and there are subtle differences between versions in the handling of macros. But this works for me (using GNUMake 3.81):

define TEST_MACRO
    $(info A $(1))
    test_var := $(1)
    $$(info B $$(test_var))
endef

$(eval $(call TEST_MACRO,test))

Upvotes: 4

Related Questions