0
votes

I have a makefile with separate rules that set a variable of the same name. How do I set this variable to become global from within the rule so that I can use it later.

rule1: test_var=/path/to/folder/

rule2: test_var=/path/to/another/folder/

$(info test_var is $(test_var))

When I call "make rule2" it says "test var is "

But I want "test var is /path/to/another/folder"

Thanks!

EDIT I may have simplified my approach too much, I thought I had it with the global variable idea but make maybe shouldn't be used that way. Here is the problem below.

When I run this code I get the error *** No rule to make target '/file_i_want_compiled.c', needed by 'out/IceCream'. Stop

LINK:= -lpthread
C_SRCS = 
C_SRCS += folder1/file1.c
C_FLAGS:= -stf=gnu11

OBJ_PATH= out/objects/

rule1: path = /path/to/rule1folder
.PHONY : rule1
rule2: path = /path/to/rule2folder
.PHONY : rule2

C_SRCS_NO_DIR := $(notdir $(C_SRCS))
C_OBJS        := $(C_SRCS_NO_DIR:%.c=$(OBJ_PATH)%.o
C_OBJS        += $(path)/file_i_want_compiled.o

out/IceCream: GEN_ICECREAM $(C_OBJS) $(OBJ_PATH)IceCream.o
    $(CC) $(C_FLAGS) $(C_OBJS) %(OBJ_PATH)IceCream.o -o $@ $(LINK)

/path/to/rule1folder & /path/to/rule2folder both have their own unique file_i_want_compiled.c

I call make with make rule1 for example

1
What is the real problem something like this is supposed to solve? Why do you want to do this?Some programmer dude
Updated with better explanation, thanks.D. Rusk
It seems like we're still missing some information here. In the snippet you have posted, the only thing running make rule1 will do is set the path variable (if that). How is the out/IceCream target reached?Shawn

1 Answers

0
votes

You're actually setting the variable correctly, but the info statement is not being evaluated in the context of the recipe. You can check this by looking at the very lengthy output of make -d rule1. Notice that the info statement is evaluated before any rules.

You can have it print out in the context of the rule by doing something like

rule1: test_var=/path/to/folder/

rule2: test_var=/path/to/another/folder/

rule%:
    $(info test_var is $(test_var))