Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't assign variable inside recipe

How do I make this work? It errors out with "make: somevariable: Command not found"

sometarget:     somevariable = somevalue 

Full example:

CXXFLAGS = -I/usr/include/test -shared -fPIC  OBJ = main.o Server.o  blabla : $(OBJ)  ifeq ($(argsexec),true)      # Creates an executable     CXXFLAGS = -I/usr/include/test     $(CXX) -o blabla $(OBJ) $(CXXFLAGS)  else      # Creates a library     DESTDIR = /home/pc     $(CXX) -o blabla $(OBJ) $(CXXFLAGS)      ./bn.sh endif 
like image 878
Blub Avatar asked Jun 29 '11 10:06

Blub


2 Answers

I found a solution using the eval function:

$(eval variablename=whatever) 

This works :)

(although I may now try to find an easier build system ;))

Thanks everyone for reading and also of course @eriktous for writing!

like image 188
Blub Avatar answered Sep 18 '22 17:09

Blub


If you write it like you did, the assignment will be executed as a shell command, which gives the error you got.

I would try organising it something like this:

 CXXFLAGS = -I/usr/include/test ifneq ($(argsexec),true)    CXXFLAGS += -shared -fPIC   DESTDIR = /home/pc endif  OBJ = main.o Server.o  blabla : $(OBJ)      $(CXX) -o blabla $(OBJ) $(CXXFLAGS)  ifneq ($(argsexec),true)      ./bn.sh endif 

This should do what you want, although I'm not quite happy with using the ifneq construct twice. I'd have to think harder to come up with something that avoids that.

like image 41
eriktous Avatar answered Sep 19 '22 17:09

eriktous