Compile all C files in a directory into separate p

2019-01-16 19:21发布

Is there a way using GNU Make of compiling all of the C files in a directory into separate programs, with each program named as the source file without the .c extension?

3条回答
老娘就宠你
2楼-- · 2019-01-16 19:30

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.

查看更多
神经病院院长
3楼-- · 2019-01-16 19:31
SRCS = $(wildcard *.c)

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

all: $(PROGS)

%: %.c

        $(CC) $(CFLAGS)  -o $@ $<
查看更多
Lonely孤独者°
4楼-- · 2019-01-16 19:46
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.

查看更多
登录 后发表回答