Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile all C files in a directory into separate programs

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?

like image 994
Martin Broadhurst Avatar asked Apr 24 '10 20:04

Martin Broadhurst


People also ask

What is separate compilation in C?

Separate compilation is an integral part of the standard for the C programming language. When a C source code file is compiled there are two tasks performed by the compiler. First, the file is compiled into a format called an object file.


2 Answers

SRCS = $(wildcard *.c)  PROGS = $(patsubst %.c,%,$(SRCS))  all: $(PROGS)  %: %.c          $(CC) $(CFLAGS)  -o $@ $< 
like image 104
Martin Broadhurst Avatar answered Oct 18 '22 20:10

Martin Broadhurst


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.

like image 25
Carl Norum Avatar answered Oct 18 '22 21:10

Carl Norum