Question

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.

Was it helpful?

Solution

PROGS := $(patsubst %.c,%,$(SRCS))

all: $(PROGS)

clean:
    /bin/rm $(PROGS)

shall do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top