Rules With Multiple Outputs in GNU Make

Mr. Make

The GNU Make "right" way
In GNU Make syntax there is really only one correct way to get a single rule with multiple outputs, and that is to use a pattern rule:

all: parser.h parser.c

%.h %.c: %.i
@echo Generating $*.h and $*.c from $*.i
@sleep 2
@touch $*.c $*.h

In direct contrast to the first example, this actually will create a single rule that creates two outputs. If you run with gmake -j 2, you'll see the files are updated only once:

Generating parser.h and parser.c from parser.i

This is the only construct that produces the correct dependency graph and behavior.  Unfortunately, it has one significant shortcoming: It requires that the input and all the outputs share a common stem, such as parser in our simple examples, so it's not as flexible as we'd like it to be.  Still, if your files do fit this restriction, then this is the best solution for you.

Conclusion
Now you know some of the ways to create a makefile that generates multiple outputs from a single command.  If possible, you should use a pattern rule with multiple outputs.  If that doesn't work for you, hopefully one of the alternatives I've shown you will.

About the Author
Eric Melski was part of the team that founded Electric Cloud and is now Architect for ElectricAccelerator. Before Electric Cloud, he was a Software Engineer at Scriptics, Inc. and Interwoven. He holds a BS in Computer Science from the University of Wisconsin.

About the author

Eric Melski's picture
Eric Melski

Eric is Chief Architect for ElectricAccelerator, a high-performance implementation of make from Electric Cloud, Inc.  He obtained a BS in Computer Science from the University of Wisconsin in Madison in 1999.  In 2002 Eric co-founded Electric Cloud, where he has spent more than a decade developing distributed, parallel systems designed to accelerate build processes.  He is named on seven patents related to his work on build acceleration at Electric Cloud.