1

I need to process all files with a particular extension, and package up the results into a single archive. Is there a way to have make do this?

For example suppose I need to take all the .w files and produce a .n file for each one with any vowels turned to periods, then tar up the results. If I create a Makefile as follows:

novowels.tar: %.n
    tar cf $@ $^

%.n: %.w
    sed -e "s/[aeiou]/./g" > $@ < $<

make (unsurprisingly) returns the following if there are no .n files in the directory:

make: *** No rule to make target '%.n', needed by 'novowels.tar'.  Stop.

I can call make the.n and it'll create the file from the.w (assuming that exists), and if I then call make it'll get included in the archive.

The question is: how (best) to have make realise that there are a whole load of .w files that need converting to .n files in the first place?

Rob Gilliam
  • 2,680
  • 4
  • 26
  • 29

1 Answers1

3

Take this as an example:

ALLFILES_IN  := $(wildcard *.w)
ALLFILES_OUT := $(ALLFILES_IN:.w=.n)


$(info In : $(ALLFILES_IN))
$(info Out: $(ALLFILES_OUT))

%.n: %.w 
   cat $< > $@

all.tar: $(ALLFILES_OUT)
  @echo "Tar:" $^

Maybe you find something like that helpful:

CPP_FILES := $(wildcard src/*.cpp)
OBJ_FILES := $(addprefix obj/,$(notdir $(CPP_FILES:.cpp=.o)))

which was stolen from: Can I compile all .cpp files in src/ to .o's in obj/, then link to binary in ./?

Community
  • 1
  • 1
Klaus
  • 24,205
  • 7
  • 58
  • 113
  • Brilliant! I knew there'd be wildcards involved, but it didn't occur to make a list of the input files then change the extensions to get the output files and make that the pre-req. – Rob Gilliam Jul 30 '14 at 19:52