akshita007
akshita007

Reputation: 619

Running an executable with input using makefile

I have the following makefile:

CC=g++
CFLAGS=-I.
tiling_file=blocking.cpp
sequential_file=sequential.cpp
n1 = 100
n2=7 
n3=4

all: tiling sequential run
tiling:
    $(CC) $(tiling_file) -fopenmp -o block

sequential: 
    $(CC) $(sequential_file) -fopenmp -o seq

run:
    ./block $(n1) $(n2) $(n3)

the block executable takes in three inputs(as specified by n1,n2,n3) .However when I execute make I get the following ouput

g++ blocking.cpp -fopenmp -o block
g++ sequential.cpp -fopenmp -o seq
./block 100 7 4 

The executable doesn't take the input unless I type 100 7 4 again and press enter. How can I run it ?

Upvotes: 1

Views: 2269

Answers (1)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136425

The executable doesn't take the input unless I type 100 7 4 again and press enter. How can I run it?

That executable probably expects data in its standard input rather than command line arguments:

run:
    echo "$(n1) $(n2) $(n3)" | ./block 

I would normally have the following rule for running executables:

run_% : %
    echo "${$*.stdin}" | ./$< ${$*.args}
.PHONY: run_%

And then I would define an executable:

mytest : # something that builds mytest executable
mytest.stdin := "this goes into the standard input of mytest"
mytest.args := --verbose --dry-run

And invoke make like this:

make run_mytest

Another point is that your recipes must produce the file they promise to produce. Presently, it promises to build a file named tiling, but builds one named block instead.

Fixes:

tiling:
    $(CC) $(tiling_file) -fopenmp -o $@

sequential: 
    $(CC) $(sequential_file) -fopenmp -o $@

In the above $@ stands for target name, tiling and sequential correspondingly.

Upvotes: 2

Related Questions