Jingguo Yao
Jingguo Yao

Reputation: 8006

How to remove file name extension in a loop with Make?

Assume that there is a directory which contains foo.c, bar.c and Makefile. The content of Makefile is:

loop:
    @for f in $(wildcard *.c) ; do \
        echo $$f ; \
        echo $(basename $$f); \
    done

Running make prints out the following text:

bar.c
bar.c
foo.c
foo.c

What I want is:

bar.c
bar
foo.c
foo

So it seems that basename has no effect in this loop. How can I make basename work?

Upvotes: 2

Views: 3256

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57183

Your loop is not working in make, it's a bash loop. If you want similar behavior in makeeeze, you can have

loop:
    $(foreach f, $(wildcard *.c), \
    echo $f; \
    echo $(basename $f);)

Or even

loop:
    $(foreach f, $(wildcard *.c), \
    $(info $f) \
    $(info $(basename $f)))

The basheese way to strip prefix is:

loop:
    @for f in $(wildcard *.c) ; do \
        echo $$f ; \
        echo $${f%.c} ; \
    done

Upvotes: 4

Related Questions