Chris
Chris

Reputation: 31266

makefile to run the code it compiles

If I have a code that will run, call it main.cpp, and the executable is r.exe, then I write a makefile with the following target:

compile: 
    g++ -std=c++11 main.cpp -o r

The executable, r.exe takes as two arguments i.txt, o.txt. How do I add a second target to the makefile such that I can run the following command, and see the program execute:

make run i.txt o.txt

I have tried adding a second target to the makefile:

run:
    r.exe $1 $2

for instance, but make declares: "'r' is up to date" and "nothing to be done for 'i.txt', ... etc."

I have also tried searching for a while now, but 'make', 'run' and 'variables' or 'arguments' have, essentially, a search firewall of unrelated content.

Upvotes: 2

Views: 3137

Answers (1)

Barry
Barry

Reputation: 303576

You can't pass arguments to make like that. The command make run i.txt o.txt would attempt to build the rules run, i.txt, and o.txt.

What you could instead to is use a variable:

run:
    r.exe ${ARGS}

make run ARGS="i.txt o.txt"

Side-note, rules should make the files that they say they do. So you really would want your compile rule to look like:

r.exe : main.cpp
    g++ -std=c++11 $^ -o $@

compile : r.exe
.PHONY  : compile

Upvotes: 5

Related Questions