[Techtalk] Loop/wildcard-like syntax for GNU make?

John Clarke johnc+linuxchix at kirriwa.net
Wed Apr 18 02:30:47 UTC 2007

On Tue, Apr 17, 2007 at 05:51:10 -0700, Kelly Jones wrote:

Hi Kelly,

> How do I generalize this to apply to ALL the GIFs in a given
> directory? I tried:
> *.jpg: *.gif
>        /usr/bin/local/bin/convert $1.gif $1.jpg

Something like this should do it:

    # define where to find the "convert" program
    CONVERT = /usr/bin/local/bin/convert

    # use the wildcard function to find all GIF files in the current
    # directory
    GIFS = $(wildcard *.gif)

    # use pattern subsitition to change the extension ".gif" to ".jpg"
    # in all filenames in ${GIFS}
    JPEGS = $(patsubst %.gif,%.jpg,${GIFS})
    # define the default target as requiring all JPEGs to be built
    all: ${JPEGS}

    # define a pattern rule to convert any GIF to a JPEG
    %.jpg: %.gif
        @echo "Converting $< to $@"
        @${CONVERT} $< $@

> (again, didn't really expect it to, since you can't put shell looping
> into a Makefile).

You can, but only in the shell function or in commands used to rebuild a
target.  GNU make also has a foreach function.  Run "info make" for all
the gory details.


If you ever want to check out the IQ of your typical skript-kiddie, take
a look at the IRC logs at honeynet.org. They make MCSEs look bright.
            -- Lionel Lauer

More information about the Techtalk mailing list