Learning Make with the Towers of Hanoi

[article]

of the : (e.g. foo.o) and $< to the file on the right-hand side (e.g. foo.c). As before $* is set to the part of the filename that is matched by the % (e.g. foo). No Phony PatternsEarlier I mentioned that the all target is phony because it's not a real file and we don't want Make confused if there were a real file called all in the current directory. The same is true for the other targets in this example. For example, if there were a file called a-c.D then we'd never see the messages for moving a disc from peg a to peg c. What you'd like to specify is something like:

.PHONY: a-c.%

so that any movement of discs from a to c would be considered phony. Unfortunately, there's no such syntax in GNU Make, .PHONY can only be used with explicit target names and trying to specify all the possible combinations of a, b, c and different numbers of D's would be impossible. ConclusionThe designers of Make didn't intend it to be used in this way, but it's a good example of the power of GNU Make that it can solve a puzzle like the Towers of Hanoi. Next month I'll get back to more mundane topics and show you a single character you can insert into your Makefiles to speed them up.

About the author

John Graham-Cumming's picture John Graham-Cumming

John Graham-Cumming is Co-Founder at Electric Cloud, Inc . Prior to joining Electric Cloud, John was a Venture Consultant with Accel Partners, VP of Internet Technology at Interwoven, Inc. (IWOV), VP of Engineering at Scriptics Corporation (acquired by Interwoven), and Chief Architect at Optimal Networks, Inc. John holds BA and MA degrees in Mathematics and Computation and a Doctorate in Computer Security from Oxford University. John is the creator of the highly acclaimed open source POPFile project. He also holds two patents in network analysis and has others pending.

AgileConnection is one of the growing communities of the TechWell network.

Featuring fresh, insightful stories, TechWell.com is the place to go for what is happening in software development and delivery.  Join the conversation now!

Upcoming Events

May 04
May 04
May 04
Jun 01