Building a Dynamic Library with GNU Make


You wish to use GNU make to build a dynamic library from a collection of C++ source files, such as those listed in Example 1-2.


First, create a makefile in the directory where you want your dynamic library to be created, and declare a phony target all whose single prerequisite is the dynamic library. Next, declare your dynamic library target. Its prerequisites should be the object files from which the library will be built, and its command script should be a command line to build the library from the collection of object files, as demonstrated in Recipe 1.4. If you are using GCC or a compiler with similar command-line syntax, customize the implicit patterns rules, if necessary, by modifying one or more of the variables CXX, CXXFLAGS, etc. used in make's database of implicit rules, as shown in Recipe 1.15. Otherwise, write a pattern rule telling make how to compile .cpp files into object files, using the command lines from Table 1-4 and the pattern rule syntax explained in Recipe 1.16. Finally, add install and clean targets, as demonstrated in Recipe 1.15, and machinery to automatically generate source file dependencies, as demonstrated in Recipe 1.16.

For example, to build a dynamic library from the source files listed Example 1-2 using GCC on Unix, create a makefile in the directory georgeringo, as shown in Example 1-22.

Example 1-22. Makefile for using GCC

# Specify extensions of files to delete when cleaning

# Specify the source files, the target files, 
# and the install directory 
SOURCES = george.cpp ringo.cpp georgeringo.cpp
INSTALLDIR = ../binaries

.PHONY: all

# Build from george.o, ringo.o, 
# and georgeringo.o; subst is the search-and-replace 
# function demonstrated in Recipe 1.16
$(OUTPUTFILE): $(subst .cpp,.o,$(SOURCES)) 
 $(CXX) -shared -fPIC $(LDFLAGS) -o $@ $^

.PHONY: install
 mkdir -p $(INSTALLDIR)

.PHONY: clean 
 for file in $(CLEANEXTS); do rm -f *.$$file; done

# Generate dependencies of .ccp files on .hpp files
include $(subst .cpp,.d,$(SOURCES))

%.d: %.cpp
 $(CC) -M $(CPPFLAGS) $< > $@.$$$$; 
 sed 's,($*).o[ :]*,1.o $@ : ,g' < $@.$$$$ > $@; 
rm -f $@.$$$$



The makefile in Example 1-22 is a straightforward application of the ideas from Recipe 1.4, Recipe 1.15, and Recipe 1.16. The main difference between Example 1-22 and Example 1-20 is the rule for building from the object files george.o, ringo.o, and georgeringo.o:

$(OUTPUTFILE): $(subst .cpp,.o,$(SOURCES)) 
 $(CXX) -shared -fPIC $(LDFLAGS) -o $@ $^

Here $(OUTPUTFILE) expands to and the expression $(subst .cpp,.o,$(SOURCES)) expands to george.o, ringo.o, and georgeringo.o, as illustrated in Recipe 1.16. The command script $(CXX) -shared -fPIC $(LDFLAGS) -o $@ $^ is an adaptation of the GCC command line presented in Table 1-7.

See Also

Recipe 1.4, Recipe 1.9, Recipe 1.12, Recipe 1.15, and Recipe 1.23

Building C++ Applications

Code Organization


Strings and Text

Dates and Times

Managing Data with Containers



Exceptions and Safety

Streams and Files

Science and Mathematics






C++ Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2006
Pages: 241 © 2008-2020.
If you may any questions please contact us: