Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through files in a Makefile

Tags:

c++

c

makefile

So I have a C++ project with a directory full of test source files and I'm writing a makefile to make them all at once. Instead of compiling each file separately, is there a way I can cd into my test directory and loop through each file, compiling them one by one?

Thanks in advance for the help!

like image 996
Jrw3 Avatar asked Aug 03 '14 05:08

Jrw3


1 Answers

makefile implicit rule can be used to make all the files which is available in the folder. Also wildcard function can be used to get the .c or .cpp files which is available in particular folder.

%.o : %.cpp
    $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@

Example:

FILENAME:=  $(patsubst %.c,%.o,$(wildcard *.c))

all:$(FILENAME)
    @echo $(FILENAME)
##write exe generation script here

%.o : %.c
    gcc -c   $< -o $@
like image 82
mahendiran.b Avatar answered Oct 07 '22 13:10

mahendiran.b