January 22, 2019

Beyond Makefiles: GNU make is For More Than Just Compiling - page 2

make and LaTex

  • June 15, 2010
  • By Juliet Kemp

Another possible use is to check a Perl script (or Apache config file, or...) for syntax errors:

all: checkperl

checkperl: myscript.pl
    perl -Mstrict -wc $<
This uses the 'strict' module to check your script, with warnings turned on. However, with this version, you'd have to edit the makefile to add a new dependency every time you add a new Perl file. To check all Perl files in that directory, you can use xargs:
checkperl: *.pl
	ls $? | xargs -L1 perl -Mstrict -wc
The implicit variable $? concatenates the names of all the source files (here everything ending .pl) with a space between them. The command then runs ls to pass these names into xargs, which then runs the perl checking command on each. (Note the -L1 option; without this, the command will run only on the first file.)

If you run this a couple of times, you'll notice that the check always runs, whether or not any *.pl files have been edited. This is, as discussed in the LaTeX example, because there is no target file checkperl for the update time to be checked again.

Other possibilities include emailing changed files to yourself for an informal backup. The variable $? lists only the files which have changed since the last time the target was generated, separated by a space. Remember that if you use a phony target (as with checkperl above), this will always list all files.


make is far more versatile and productive than you might have thought from only encountering it when building the odd piece of software from source. It's well worth experimenting with for any repetitive file-processing task you find yourself doing. Check out the GNU make manual for extensive further information, in particular the sections on variables and implicit rules.

Most Popular LinuxPlanet Stories