Reputation: 36554
I have a bunch of C files in a directory (each file named such that it starts with the characters program
, e.g. program1.c
, program2.c
, program3.c
) and I intend for each of these C files to compile into its respective binary (program1
, program2
, program3
).
How would I write a Makefile
to achieve this so I can simply issue make
to create multiple binaries? (Using a loop instead of manually stating a make statement for each c source file)
Here's my attempt:-
CFLAGS=-Wall -g
SRCS=$(wildcard program*.c)
all:
for F in *.c; do \
F=$${$$F.%c}; make $$F; done
clean:
rm -f program1
rm -f program2
rm -f program3
which gives me a bad subsitution
error.
Upvotes: 1
Views: 462
Reputation: 7923
PROGS := $(patsubst %.c,%,$(SRCS))
all: $(PROGS)
clean:
/bin/rm $(PROGS)
shall do it.
Upvotes: 4