0

I'm trying to figure out how to run a program with inputs from the command line using a makefile.

For example, let's say I have a program foo that takes in two integers and adds them and returns the result. In the make file when I type in make test for example it'll go into my makefile and it'd look like this

test: ./foo 1 1 ./foo 2 2 

and so on, but when I'm trying to do it it doesn't run. Is there something I am missing?

3
  • 2
    When you run the make command, are you in the directory where foo is? What if you use an absolute path to the program? Oh, and what error(s) do you get? Commented Dec 12, 2016 at 8:29
  • @Someprogrammerdude Actually no, right now I have a main folder called project and the executable is in project/program and my testing makefile is in project/testing. I assumed that I'd simply have to do ./project/program/foo 1 1 to make it work. The error I am getting is that the ./project/program/foo command was not found Commented Dec 12, 2016 at 8:31
  • And you are sure that $PWD/project/program/foo exists (and has the x-bit set)? BTW, the error complains about project/program/foo, but you have in your makefile. Maybe you are looking at the wrong place for the error. In any case, I would put a ls -l as the first command below the test target in your makefile. Also, in case foo happens to be a shell script, I would call it with -x. Maybe this foo does run, but then tries to execute a different foo in another directory, which causes the error. Commented Dec 12, 2016 at 11:06

1 Answer 1

1

You can get the absolute path of your Makefile with:

mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) 

Then, you can build your executable path with:

exe_path := $(mkfile_path)/../program 

Finally, invoque foo with its path:

$(exe_path)/foo 1 1 
Sign up to request clarification or add additional context in comments.

2 Comments

What is abspath, lastword, and MAKEFILE_LIST?
These are internal functions and variables of GNU Make (see documentation: abspath, lastword, MAKEFILE_LIST)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.