make - substitute all parameters in test objective

Viewed 13

I'd like to make a "test case" with make

Whay I expect

    greet.out -i
    # ciao
    greet.out -e
    # hello
    greet.out -f
    # Bonjour

My makefile is like

    all: build test
    
    build: main.cpp
        g++ main.cpp -o greet.out
    
    test: greet.out
        par = -i -f -e
        // here I'd like to call my app for every parameter I have
        foreach p in $par
        do
        greet.out $p
        done

What I'd prefer to avoid:

test: greet.out 
    ./greet.out -i
    ./greet.out -e
    ./greet.out -f
    
1 Answers

Everything in a recipe is a shell script. This:

par = -i -f -e

is not a valid shell variable assignment. Probably you mean:

par="-i -f -e"

Second, every logical line in a recipe is invoked in a separate shell. If you want things to run in the same shell you have to use backslashes to turn multiple physical lines into one logical line. And if you want to refer to shell variables, not make variables, you have to escape the $ as $$:

test: greet.out
        par="-i -f -e"; \
        for p in $$par; do \
            greet.out $$p; \
        done

Alternatively, you could use an actual make variable for par, but then it can't be in the recipe:

par = -i -f -e

test: greet.out
        for p in $(par); do \
            greet.out $$p; \
        done
Related