libs.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  4. <title>Dependent Targets</title>
  5. <link rel="stylesheet" href="../../boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.68.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="hierarchy.html" title="Project Hierarchies">
  19. <link rel="next" href="linkage.html" title="Static and shared libaries">
  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="hierarchy.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="linkage.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.libs"></a>Dependent Targets</h2></div></div></div>
  37. <p>
  38. Targets that are &#8220;needed&#8221; by other targets are called
  39. <em class="firstterm">dependencies</em> of those other targets. The
  40. targets that need the other targets are called
  41. <em class="firstterm">dependent</em> targets.
  42. </p>
  43. <p>To get a feeling of target dependencies, let's continue the
  44. above example and see how <code class="filename">top/app/Jamfile</code> can
  45. use libraries from <code class="filename">top/util/foo</code>. If
  46. <code class="filename">top/util/foo/Jamfile</code> contains
  47. </p>
  48. <pre class="programlisting">
  49. lib bar : bar.cpp ;
  50. </pre>
  51. <p>
  52. then to use this library in <code class="filename">top/app/Jamfile</code>, we can
  53. write:
  54. </p>
  55. <pre class="programlisting">
  56. exe app : app.cpp ../util/foo//bar ;
  57. </pre>
  58. <p>
  59. While <code class="computeroutput">app.cpp</code> refers to a regular source file,
  60. <code class="computeroutput">../util/foo//bar</code> is a reference to another target:
  61. a library <code class="filename">bar</code> declared in the Jamfile at
  62. <code class="filename">../util/foo</code>.
  63. </p>
  64. <div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;">
  65. <h3 class="title">Tip</h3>
  66. <p>Some other build system have special syntax for listing dependent
  67. libraries, for example <code class="varname">LIBS</code> variable. In Boost.Build,
  68. you just add the library to the list of sources.
  69. </p>
  70. </div>
  71. <p>Suppose we build <code class="filename">app</code> with:
  72. </p>
  73. <pre class="screen">
  74. bjam app optimization=full define=USE_ASM
  75. </pre>
  76. <p>
  77. Which properties will be used to build <code class="computeroutput">foo</code>? The answer is
  78. that some features are
  79. <em class="firstterm">propagated</em>&#8212;Boost.Build attempts to use
  80. dependencies with the same value of propagated features. The
  81. <code class="varname">&lt;optimization&gt;</code> feature is propagated, so both
  82. <code class="filename">app</code> and <code class="filename">foo</code> will be compiled
  83. with full optimization. But <code class="varname">&lt;define&gt;</code> is not
  84. propagated: its value will be added as-is to the compiler flags for
  85. <code class="filename">a.cpp</code>, but won't affect <code class="filename">foo</code>.
  86. </p>
  87. <p>Let's improve this project further.
  88. The library
  89. probably has some headers that must be used when compiling
  90. <code class="filename">app.cpp</code>. We could manually add the necessary
  91. <code class="computeroutput">#include</code> paths to <code class="filename">app</code>'s
  92. requirements as values of the
  93. <code class="varname">&lt;include&gt;</code> feature, but then this work will
  94. be repeated for all programs
  95. that use <code class="filename">foo</code>. A better solution is to modify
  96. <code class="filename">util/foo/Jamfile</code> in this way:
  97. </p>
  98. <pre class="programlisting">
  99. project
  100. : usage-requirements &lt;include&gt;.
  101. ;
  102. lib foo : foo.cpp ;
  103. </pre>
  104. <p>
  105. Usage requirements are applied not to the target being declared
  106. but to its
  107. dependents. In this case, <code class="literal">&lt;include&gt;.</code> will be applied to all
  108. targets that directly depend on <code class="filename">foo</code>.
  109. </p>
  110. <p>Another improvement is using symbolic identifiers to refer to
  111. the library, as opposed to <code class="filename">Jamfile</code> location.
  112. In a large project, a library can be used by many targets, and if
  113. they all use <code class="filename">Jamfile</code> location,
  114. a change in directory organization entails much work.
  115. The solution is to use project ids&#8212;symbolic names
  116. not tied to directory layout. First, we need to assign a project id by
  117. adding this code to
  118. <code class="filename">Jamroot</code>:</p>
  119. <pre class="programlisting">
  120. use-project /library-example/foo : util/foo ;
  121. </pre>
  122. <p>Second, we modify <code class="filename">app/Jamfile</code> to use the
  123. project id:
  124. </p>
  125. <pre class="programlisting">
  126. exe app : app.cpp /library-example/foo//bar ;
  127. </pre>
  128. <p>
  129. The <code class="filename">/library-example/foo//bar</code> syntax is used
  130. to refer to the target <code class="filename">bar</code> in
  131. the project with id <code class="filename">/library-example/foo</code>.
  132. We've achieved our goal&#8212;if the library is moved to a different
  133. directory, only <code class="filename">Jamroot</code> must be modified.
  134. Note that project ids are global&#8212;two Jamfiles are not
  135. allowed to assign the same project id to different directories.
  136. </p>
  137. <div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;">
  138. <h3 class="title">Tip</h3>
  139. <p>If you want all applications in some project to link
  140. to a certain library, you can avoid having to specify it directly the sources of every
  141. target by using the
  142. <code class="varname">&lt;source&gt;</code> property. For example, if <code class="filename">/boost/filesystem//fs</code>
  143. should be linked to all applications in your project, you can add
  144. <code class="computeroutput">&lt;source&gt;/boost/filesystem//fs</code> to the project's requirements, like this:</p>
  145. <pre class="programlisting">
  146. project
  147. : requirements &lt;source&gt;/boost/filesystem//fs
  148. ;
  149. </pre>
  150. </div>
  151. </div>
  152. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  153. <td align="left"></td>
  154. <td align="right"><small></small></td>
  155. </tr></table>
  156. <hr>
  157. <div class="spirit-nav">
  158. <a accesskey="p" href="hierarchy.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="linkage.html"><img src="../../images/next.png" alt="Next"></a>
  159. </div>
  160. </body>
  161. </html>
粤ICP备19079148号