Run a Shell Command in a Makefile

I had a directory of BMP image files that I wanted to convert to EPS (Encapsulated PostScript). Since I was planning on adding files to the directory, I did not want to hard code the names of the files into the makefile that would do the conversion. Therefore, the trick was to run a shell command inside of the makefile. The first line lists all of the BMP files and assigns it to “BMP_FILES”. The next line replaces the “bmp” extension with “eps”. The complete makefile is below.

BMP_FILES = $(shell ls *.bmp)
EPS_FILES = ${BMP_FILES:%.bmp=%.eps}

all: ${EPS_FILES}

%.eps : %.bmp
	convert $*.bmp $*.eps

All that is left is to type “make” in the directory, and all of the BMP files will be converted to EPS.

One thought on “Run a Shell Command in a Makefile”

  1. This is also helpful for use with xfig and gnuplot, in conjunction with LaTeX.

    You can create PDFs of graphs using:

    %.pdf: %.p
        gnuplot $<
        epstopdf ${@:.pdf=.eps}

    You can create PDFs from xfig files using:

    %.pdf: %.fig
        fig2dev -L pdf $< ${@:.fig=.pdf}

Leave a Reply

Your email address will not be published. Required fields are marked *