Compile all C files in a directory into separate programs

空扰寡人 提交于 2019-11-27 05:25:00

问题


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?


回答1:


SRCS = $(wildcard *.c)

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

all: $(PROGS)

%: %.c

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



回答2:


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:


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.



来源:https://stackoverflow.com/questions/2706064/compile-all-c-files-in-a-directory-into-separate-programs

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