The “shell” built-in function supported by GNU Make can be used to get the list of C source files in a directory. Then a list of corresponding object files can be created by replacing the file extension in the file list. The following example shows how to compile all C source files present in the current directory to generate an executable named “target“. If you are doing copy and paste of the following example, ensure that you place TAB for all commands under each rule. Otherwise GNU Make complains about “missing separator” error.

TARGET=target
SRCS=$(shell ls *.c)
OBJS=$(SRCS:.c=.o)

all: $(TARGET)

$(TARGET):$(OBJS)
clean:
	rm -f *.o $(TARGET)

.PHONY: all clean