Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Makefile variable inside target

Tags:

makefile

I have a target inside a makefile:

all: $(TARGETS) 

I want a variant that differs from all only by the fact that it sets an environment variable. Something like:

all-abc: $(TARGETS)     ABC=123 

but that doesn't work because the dependencies are processed before the variable is set. I've thought about having another dependency before the real ones that just sets the environment variable but I don't think the environment persists across targets. That is to say that

abc:     ABC=123 all-abc: abc $(TARGETS) 

doesn't work. What I ultimately want to be able to do is

$ make all-abc 

instead of

$ ABC=123 make 

Is it possible to set an environment variable like this ?

(GNU Make 3.82)

like image 512
starfry Avatar asked Mar 05 '13 17:03

starfry


People also ask

What is $$ in makefile?

$$ means be interpreted as a $ by the shell. the $(UNZIP_PATH) gets expanded by make before being interpreted by the shell.

What is $@ and in makefile?

The $@ and $< are called automatic variables. The variable $@ represents the name of the target and $< represents the first prerequisite required to create the output file. For example: hello.o: hello.c hello.h gcc -c $< -o $@ Here, hello.o is the output file.

How do I override a variable in makefile?

There is one way that the makefile can change a variable that you have overridden. This is to use the override directive, which is a line that looks like this: ' override variable = value ' (see The override Directive).


1 Answers

try this:

all:     @#usual rule, if you call `make all-abc`, this will print "123"     @echo $(ABC)  all-abc: ABC=123 all-abc: all     @#what you put here it's going to be executed after the rule `all` 
like image 91
RSFalcon7 Avatar answered Sep 20 '22 23:09

RSFalcon7