Compile all C files in a directory into separate programs

最后都变了- 提交于 2019-11-28 04:57:39
Martin Broadhurst
SRCS = $(wildcard *.c)

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

all: $(PROGS)

%: %.c

        $(CC) $(CFLAGS)  -o $@ $<

I don't think you even need a makefile - the default implicit make rules should do it:

$ ls
src0.c  src1.c  src2.c  src3.c
$ make `basename -s .c *`
cc     src0.c   -o src0
cc     src1.c   -o src1
cc     src2.c   -o src2
cc     src3.c   -o src3

Edited to make the command line a little simpler.

SRCS = $(wildcard *.c)

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

all: $(PROGS)

%: %.c
        $(CC) $(CFLAGS) -o $@ $<
clean: 
        rm -f $(PROGS)

Improving Martin Broadhurst's answer by adding "clean" target. "make clean" will clean all executable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!