iwahdan
iwahdan

Reputation: 449

Nested For loop in makefile

I am trying to loop through the .c files in a specific directory through the makefile.

i used the following code, but it seems not working:

DIR= Sources \
     Sources_2

@for entry in ${DIR} ;                  \
do                                         \
    @for i in $${entry}/*.c ;                \
    do                                     \
        echo "Processing $${i}";             \
        #Building Commands go here
    done                                   \
done

I get error: "/bin/sh: -c: line 3: syntax error near unexpected token `do'"

Upvotes: 4

Views: 4366

Answers (2)

Santosh A
Santosh A

Reputation: 5361

Change the Makefile as below

@for entry in ${DIR} ; do            \
    for i in $$entry/*.c ; do     \
        echo "Processing $$i";     \
        #Building Commands go here
    done                              \
done

The reason being wrong syntax usage of for loop.

Upvotes: 1

Oleg Andriyanov
Oleg Andriyanov

Reputation: 5289

You shouldn't use at sign @ near the second for loop. @ should be used at the beginning of the whole shell command. The following worked for me:

DIR= Sources \
     Sources_2

all:
    @for entry in ${DIR};                   \
    do                                      \
        for i in $${entry}/*.c;             \
        do                                  \
            echo "Processing $${i}";        \
        done                                \
    done

Upvotes: 5

Related Questions