1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Static and shared libaries</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.69.1">
<link rel="start" href="../../index.html" title="Boost.Build V2 User Manual">
<link rel="up" href="../tutorial.html" title="Chapter3.Tutorial">
<link rel="prev" href="testing.html" title="Testing">
<link rel="next" href="conditions.html" title="Conditions and alternatives">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="testing.html"><img src="../../../../doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../../../../doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="conditions.html"><img src="../../../../doc/html/images/next.png" alt="Next"></a>
</div>
<div class="section" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="bbv2.tutorial.linkage"></a>Static and shared libaries</h2></div></div></div>
<p>Libraries can be either
<span class="emphasis"><em>static</em></span>, which means they are included in executable
files that use them, or <span class="emphasis"><em>shared</em></span> (a.k.a.
<span class="emphasis"><em>dynamic</em></span>), which are only referred to from executables,
and must be available at run time. Boost.Build can create and use both kinds.
</p>
<p>The kind of library produced from a <code class="computeroutput">lib</code> target is
determined by the value of the <code class="varname">link</code> feature. Default
value is <code class="literal">shared</code>, and to build static library, the value
should be <code class="literal">static</code>. You can either requiest static build
on the command line:
</p>
<pre class="screen">
bjam link=static
</pre>
<p>
or in the library's requirements:
</p>
<pre class="programlisting">
lib l : l.cpp : <link>static ;
</pre>
<p>
</p>
<p>
We can also use the <code class="varname"><link></code> property
to express linking requirements on a per-target basis.
For example, if a particular executable can be correctly built
only with the static version of a library, we can qualify the
executable's <a href="../reference/definitions.html#bbv2.reference.targets.references">target
reference</a> to the library as follows:
</p>
<pre class="programlisting">
exe important : main.cpp helpers/<link>static ;</pre>
<p>
No matter what arguments are specified on the <span><strong class="command">bjam</strong></span>
command line, <code class="filename">important</code> will only be linked with
the static version of <code class="filename">helpers</code>.
</p>
<p>
Specifying properties in target references is especially useful if you
use a library defined in some other project (one you can't
change) but you still want static (or dynamic) linking to that library
in all cases. If that library is used by many targets,
you <span class="emphasis"><em>could</em></span> use target references
everywhere:
</p>
<pre class="programlisting">
exe e1 : e1.cpp /other_project//bar/<link>static ;
exe e10 : e10.cpp /other_project//bar/<link>static ;</pre>
<p>
but that's far from being convenient. A better approach is
to introduce a level of indirection. Create a local
<span class="type">alias</span> target that refers to the static (or
dynamic) version of <code class="filename">foo</code>:
</p>
<pre class="programlisting">
alias foo : /other_project//bar/<link>static ;
exe e1 : e1.cpp foo ;
exe e10 : e10.cpp foo ;</pre>
<p>
The <a href="../tasks/alias.html" title="Alias"><code class="computeroutput">alias</code></a>
rule is specifically used to rename a reference to a target and possibly
change the properties.
</p>
<div class="tip"><table border="0" summary="Tip">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../doc/html/images/tip.png"></td>
<th align="left">Tip</th>
</tr>
<tr><td align="left" valign="top">
<p>
When one library uses another, you put the second library in
the source list of the first. For example:
</p>
<pre class="programlisting">
lib utils : utils.cpp /boost/filesystem//fs ;
lib core : core.cpp utils ;
exe app : app.cpp core ;</pre>
<p>
This works no matter what kind of linking is used. When
<code class="filename">core</code> is built as a shared library, it is linked
directly into <code class="filename">utils</code>. Static libraries can't
link to other libraries, so when <code class="filename">core</code> is built
as a static library, its dependency on <code class="filename">utils</code> is passed along to
<code class="filename">core</code>'s dependents, causing
<code class="filename">app</code> to be linked with both
<code class="filename">core</code> and <code class="filename">utils</code>.
</p>
</td></tr>
</table></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../doc/html/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>(Note for non-UNIX system). Typically, shared libraries must be
installed to a directory in the dynamic linker's search
path. Otherwise, applications that use shared libraries can't be
started. On Windows, the dynamic linker's search path is given by the
<code class="envar">PATH</code> environment variable. This restriction is lifted
when you use Boost.Build testing facilities—the
<code class="envar">PATH</code> variable will be automatically adjusted before
running executable.
</p></td></tr>
</table></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><small></small></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="testing.html"><img src="../../../../doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../../../../doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="conditions.html"><img src="../../../../doc/html/images/next.png" alt="Next"></a>
</div>
</body>
</html>
|