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