Painless non-recursive Make

[article]

rm -rf /tmp/out/library

rm -rf /tmp/out/executable

Asking for the sublibrary to be built:

$ cd /src

$ make -n clean-library_sublibrary

rm -rf /tmp/out/library/sublibrary

$ make -n library_sublibrary

cc    -c -o '/tmp/out/library/sublibrary/slib1.o' '/home/jgc/doc/nonrecursive/library/sublibrary/slib1.c'

cc    -c -o '/tmp/out/library/sublibrary/slib2.o' '/home/jgc/doc/nonrecursive/library/sublibrary/slib2.c'

ar r '/tmp/out/library/sublibrary/slib.a' /tmp/out/library/sublibrary/slib1.o /tmp/out/library/sublibrary/slib2.o

ranlib '/tmp/out/library/sublibrary/slib.a'

And if we ask that the executable module be built the library gets built at the same time because of the dependency (and also the sublibrary):

$ cd /src/executable

$ make -n executable

cc    -c -o '/tmp/out/library/sublibrary/slib1.o' '/home/jgc/doc/nonrecursive/library/sublibrary/slib1.c'

cc    -c -o '/tmp/out/library/sublibrary/slib2.o' '/home/jgc/doc/nonrecursive/library/sublibrary/slib2.c'

ar r '/tmp/out/library/sublibrary/slib.a' /tmp/out/library/sublibrary/slib1.o /tmp/out/library/sublibrary/slib2.o

ranlib '/tmp/out/library/sublibrary/slib.a'

cc    -c -o '/tmp/out/library/lib1.o' '/home/jgc/doc/nonrecursive/library/lib1.c'

cc    -c -o '/tmp/out/library/lib2.o' '/home/jgc/doc/nonrecursive/library/lib2.c'

ar r '/tmp/out/library/lib.a' /tmp/out/library/lib1.o /tmp/out/library/lib2.o

ranlib '/tmp/out/library/lib.a'

cc    -c -o '/tmp/out/executable/foo.o' '/home/jgc/doc/nonrecursive/executable/foo.c'

cc    -c -o '/tmp/out/executable/bar.o' '/home/jgc/doc/nonrecursive/executable/bar.c'

g++     /tmp/out/executable/foo.o /tmp/out/executable/bar.o /tmp/out/library/lib.a -o'/tmp/out/executable/exec'

Conclusion

Although nothing like as simple as recursive Make, this non-recursive system is
very flexible (allowing dependencies between individual binary files across modules; that's not possible with recursive Make) without losing the 'go to any directory and type Make' that engineers are familiar with.

Let me know if you use it, or improve on it.

About the author

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!