Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append value to variable in make file

I have a definitions.mk file with some definitions:

define some-method

if [ ! -f <some file> ] ; then
    MYVAR += text_to_append

It is the line with MYVAR that is my problem. It thinks MYVAR is a command. How can I make it realize that it is the variable MYVAR (which also exists in other make files) that I am referring to?

Thanks in advance for any input!

like image 255
Liz Avatar asked Aug 31 '11 10:08

Liz


People also ask

How do I append a variable in makefile?

makefile Variables Appending Text To an Existing Variable The += operator is a common extension that adds the specified content to the end of the variable, separated by a space. Variable references in the right-hand side will be expanded if and only if the original variable was defined as a simply-expanded variable.

What does += mean in makefile?

+= is used for appending more text to variables e.g. objects=main.o foo.o bar.o. objects+=new.o. which will set objects to 'main.o foo.o bar.o new.o' = is for recursively expanded variable.

How do I use IFEQ in makefile?

The ifeq directive begins the conditional, and specifies the condition. It contains two arguments, separated by a comma and surrounded by parentheses. Variable substitution is performed on both arguments and then they are compared.

How do you append to a variable in Python?

In python, to append a string in python we will use the ” + ” operator and it will append the variable to the existing string.


1 Answers

You can't use a shell-style "if" statement in a Makefile. You need to use the GNU make conditional syntax.

Something like:

ifneq ($(wildcard some_file),)
# File exists
MYVAR += text_to_append
endif

Also, do not use tabs for indentation in your Makefile, they have special meaning to Make.

like image 148
user9876 Avatar answered Oct 08 '22 21:10

user9876