| Home | Libraries | People | FAQ | More

## Static and shared libaries

While the previous section explained how to create and use libraries, it omitted one important detail. Libraries can be either static, which means they are included in executable files which use them, or shared (a.k.a. dynamic), which are only referred to from executables, and must be available at run time. Boost.Build can work with both types. By default, all libraries are shared. This is much more efficient in build time and space. But the need to install all libraries to some location is not always convenient, especially for debug builds. Also, if the installed shared library changes, all application which use it might start to behave differently.

Static libraries do not suffer from these problems, but considerably increase the size of application. Before describing static libraries, it's reasonable to give another, quite simple approach. If your project is built with <hardcode-dll-paths>true property, then the application will include the full paths for all shared libraries, eliminating the above problems. Unfortunately, you no longer can move shared library to a different location, which makes this option suitable only for debug builds. Further, only gcc compiler supports this option.

Building a library statically is easy. You'd need to change the value of <link> feature from it's deafault value `shared`, to `static`. So, to build everything as static libraries, you'd say

    bjam link=static

on the command line. The linking mode can be fine-tuned on per-target basis.

1. Suppose your library can be only build statically. This is easily achieved using requirements:

       lib l : l.cpp : <link>static ;

2. What if library can be both static and shared, but when using it in specific executable, you want it static? Target references are here to help:

       exe important : main.cpp helpers/<link>static ;

3. What if the library is defined in some other project, which you cannot change. But still, you want static linking to that library in all cases. You can use target references everywhere:

       exe e1 : e1.cpp /other_project//bar/<link>static ;
       exe e10 : e10.cpp /other_project//bar/<link>static ;

   but that's far from being convenient. Another way is to introduce a level of indirection: create a local target, which will refer to static version of `foo`. Here's the solution:

       alias foo : /other_project//bar/<link>static ;
       exe e1 : e1.cpp foo ;
       exe e10 : e10.cpp foo ;

   Note that the alias rule is specifically used for rename a reference to a target and possibly change the properties.