DanWaller
DanWaller

Reputation: 13

Passing variables from terminal to makefile

I'm new to Linux Mint and was wondering if it was possible if in my make file i could create a variable and then when i call the function(not sure what you would call it here) such as run or all with this variable. WHat i'm basically trying to do is from the terminal i want to do something like this:

make open(variable)

which would then pass that variable into the makefile where something like:

all:

    vim $(filename)

would receive it and open the file in Vim.

Upvotes: 1

Views: 595

Answers (2)

missimer
missimer

Reputation: 4079

In your vim example you would want:

make all filename=the-file.txt

Note that the all can be omitted assuming it is the first target in your Makefile. Basically the syntax is variable=value

This is better demonstrated with the following Makefile:

echo:
    echo $(MY_ECHO)

if you do: make MY_ECHO=hello the variable MY_ECHO will be the string hello.

How you specify a target or targets is the same as when you do not use variables, you just need to make sure you have variable=value without any spaces so make knows whether it is a variable or target. For example if you do:

make MY_ECHO= asdf

You will get the error:

make: *** No rule to make target 'asdf'.  Stop.

As make will think asdf is a target. If you want to use a value with a space you need to surround it with quotes:

make MY_ECHO="hello world"

Upvotes: 3

dbush
dbush

Reputation: 223739

Given your example makefile, you would call make from the command line as:

make filename=/tmp/my/file

Then /tmp/my/file would open in vim.

Upvotes: 3

Related Questions