Building a Dynamic Library Using Boost.Build


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


Create a Jamroot file in the directory where you wish the dynamic libraryand the import library, if anyto be created. In the file Jamroot, invoke the lib rule to declare a library target, specifying your .cpp files as sources and the properties

shared as a requirement. Add a usage requirement of the form path to specify the library's include directory, i.e., the directory with respect to which include directives for library headers should be resolved. If your source files include headers from other libraries, you may need to add several requirements of the form path to tell the compiler where to search for included headers. You may also need to add one or more requirements of the form symbol to ensure that your dynamic library's symbols will be exported using _ _declspec(dllexport) on Windows. Finally, run bjam from the directory containing Jamroot, as described in Recipe 1.7.

For example, to build a dynamic library from the source files listed in Example 1-2, create a file named Jamroot in the directory georgeringo, as shown in Example 1-12.

Example 1-12. A Jamfile to build the dynamic library, georgeringo.dll, or georgeringo.dylib

# Jamfile for project georgringo

lib libgeorgeringo
 : # sources
 george.cpp ringo.cpp georgeringo.cpp
 : # requirements

shared GEORGERINGO_DLL : # default-build : # usage-requirements .. ;

To build the library, enter:

> bjam libgeorgeringo



As discussed in Recipe 1.8, the lib rule is used to declare a target representing a static or dynamic library. The usage requirement .. frees projects which depend on your library from having to explicitly specify your library's include directory in their requirements. The requirement

shared specifies that the target should always be built as a dynamic library. If you want the freedom to build a library target either as static or as dynamic, you can omit the requirement

shared and specify this property on the command line, or in the requirements of a target that depends on the library target. Writing a library which can be built as either static or dynamic requires some care, however, because of the preprocessor directives necessary to ensure that symbols are properly exported on Windows. Rewriting Example 1-2 so that it can be built as either static or dynamic makes a good exercise.

See Also

Recipe 1.4, Recipe 1.12, Recipe 1.16, and Recipe 1.19

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: