separate_compilation.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Guidelines for Authors of Boost Libraries Containing Separate Source</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <LINK href="../boost.css" type="text/css" rel="stylesheet"></head>
  7. <body>
  8. <TABLE summary="Page header" id="Table1" cellSpacing="1" cellPadding="1" width="100%" border="0">
  9. <TR>
  10. <td vAlign="top" width="300">
  11. <h3><A href="../index.htm"><IMG height="86" alt="C++ Boost" src="../boost.png" width="277" border="0"></A></h3>
  12. </td>
  13. <TD width="353">
  14. <H1 align="center">Guidelines for Authors of Boost Libraries Containing Separate
  15. Source</H1>
  16. </TD>
  17. </TR>
  18. </TABLE>
  19. <BR>
  20. <HR>
  21. <P>These guidelines are designed for the authors of Boost libraries which have
  22. separate source that need compiling in order to use the library. Throughout,
  23. this guide refers to a fictitious "whatever" library, so replace all
  24. occurrences of "whatever" or "WHATEVER" with your own library's name when
  25. copying the examples.</P>
  26. <H2>Contents</H2>
  27. <dl class="index">
  28. <dt><A href="#source_changes">Changes Affecting Source Code</A>
  29. <dd>
  30. <dl class="index">
  31. <dt><A href="#abi">Preventing Compiler ABI Clashes</A> <dt><A href="#dlls">Supporting
  32. Windows Dll's</A> <dt><a href="#auto-link">Automatic Library Selection and Linking
  33. with auto_link.hpp</a> </dt>
  34. </dl>
  35. <dt><A href="#build_changes">Changes Affecting the Build System</A>
  36. <dd>
  37. <dl class="index">
  38. <dt><A href="#jamfile">Creating the Library Jamfile</A> <dt><A href="#testing">Testing
  39. Auto-linking</A> </dt>
  40. </dl>
  41. <dt><A href="#copyright">Copyright</A></dt>
  42. </dl>
  43. <h2><A name="source_changes"></A>Changes Affecting Source Code</h2>
  44. <H3><A name="abi"></A>Preventing Compiler ABI Clashes</H3>
  45. <P>There are some compilers (mostly Microsoft Windows compilers again!), which
  46. feature a range of compiler switches that alter the ABI of C++ classes and
  47. functions. By way of example, consider Borland's compiler which has the
  48. following options:</P>
  49. <PRE>-b (on or off - effects enum sizes).
  50. -Vx (on or off - empty members).
  51. -Ve (on or off - empty base classes).
  52. -aX (alignment - 5 options).
  53. -pX (Calling convention - 4 options).
  54. -VmX (member pointer size and layout - 5 options).
  55. -VC (on or off, changes name mangling).
  56. -Vl (on or off, changes struct layout).
  57. </PRE>
  58. <P>These options are provided in addition to those affecting which runtime library
  59. is used (more on which later); the total number of combinations of options can
  60. be obtained by multiplying together the individual options above, so that gives
  61. 2*2*2*5*4*5*2*2 = 3200 combinations!
  62. </P>
  63. <P>The problem is that users often expect to be able to build the Boost libraries
  64. and then just link to them and have everything just plain work, no matter what
  65. their project settings are. Irrespective of whether this is a reasonable
  66. expectation or not, without some means of managing this issue, the user may
  67. well find that their program will experience strange and hard to track down
  68. crashes at runtime unless the library they link to was built with the same
  69. options as their project (changes to the default alignment setting are a prime
  70. culprit). One way to manage this is with "prefix and suffix" headers: these
  71. headers invoke compiler specific #pragma directives to instruct the compiler
  72. that whatever code follows was built (or is to be built) with a specific set of
  73. compiler ABI settings.</P>
  74. <P>Boost.config provides the macro BOOST_HAS_ABI_HEADERS which is set whenever
  75. there are prefix and suffix headers available for the compiler in use, typical
  76. usage in a header like this:</P>
  77. <PRE>#ifndef BOOST_WHATEVER_HPP
  78. #define BOOST_WHATEVER_HPP
  79. #include &lt;boost/config.hpp&gt;
  80. // this must occur after all of the includes and before any code appears:
  81. #ifdef BOOST_HAS_ABI_HEADERS
  82. # include BOOST_ABI_PREFIX
  83. #endif
  84. //
  85. // this header declares one class, and one function by way of examples:
  86. //
  87. class whatever
  88. {
  89. // details.
  90. };
  91. whatever get_whatever();
  92. // the suffix header occurs after all of our code:
  93. #ifdef BOOST_HAS_ABI_HEADERS
  94. # include BOOST_ABI_SUFFIX
  95. #endif
  96. #endif
  97. </PRE>
  98. <P>You can include this code in your source files as well if you want - although
  99. you probably shouldn't need to - these headers fix the ABI to the default used
  100. by the compiler, and if the user attempts to compile the source with any other
  101. setting then they will get compiler errors if there are any conflicts.</P>
  102. <H4>Rationale:</H4>
  103. <P>Without some means of managing this issue, users often report bugs along the
  104. line of "Your silly library always crashes when I try and call it" and so on.
  105. These issues can be extremely difficult and time consuming to track down, only
  106. to discover in the end that it's a compiler setting that's changed the ABI of
  107. the class and/or function types of the program compared to those in the
  108. pre-compiled library. The use of prefix/suffix headers can minimize this
  109. problem, although probably not remove it completely.</P>
  110. <H5>Counter Argument #1:</H5>
  111. <P>Trust the user, if they want 13-byte alignment (!) let them have it.</P>
  112. <H5>Counter Argument #2:</H5>
  113. <P>Prefix/suffix headers have a tendency to "spread" to other boost libraries -
  114. for example if boost::shared_ptr&lt;&gt; forms part of your class's ABI, then
  115. including prefix/suffix headers in your code will be of no use unless
  116. shared_ptr.hpp also uses them. Authors of header-only boost libraries may not
  117. be so keen on this solution - with some justification - since they don't face
  118. the same problem.</P>
  119. <h3><A name="dlls"></A>Supporting Windows Dll's</h3>
  120. <p>On most Unix-like platforms no special annotations of source code are required
  121. in order for that source to be compiled as a shared library because all
  122. external symbols are exposed. However the majority of Windows compilers require
  123. that symbols that are to be imported or exported from a dll, be prefixed with
  124. __declspec(dllimport) or __declspec(dllexport). Without this mangling of source
  125. code, it is not possible to correctly build shared libraries on Windows
  126. (historical note - originally these declaration modifiers were required on
  127. 16-bit Windows where the memory layout for exported classes was different from
  128. that of "local" classes - although this is no longer an issue, there is still
  129. no way to instruct the linker to "export everything", it also remains to be
  130. seen whether 64-bit Windows will resurrect the segmented architecture that led
  131. to this problem in the first place. Note also that the mangled names of
  132. exported symbols are different from non-exported ones, so __declspec(dllimport)
  133. is required in order to link to code within a dll).</p>
  134. <p>In order to support the building of shared libraries on MS Windows your code
  135. will have to prefix all the symbols that your library exports with a macro
  136. (lets call it BOOST_WHATEVER_DECL) that your library will define to expand to
  137. either __declspec(dllexport) or __declspec(dllimport) or nothing, depending
  138. upon how your library is being built or used. Typical usage would look like
  139. this:</p>
  140. <pre>#ifndef BOOST_WHATEVER_HPP
  141. #define BOOST_WHATEVER_HPP
  142. #include &lt;boost/config.hpp&gt;
  143. #ifdef BOOST_HAS_DECLSPEC // defined in config system
  144. // we need to import/export our code only if the user has specifically
  145. // asked for it by defining either BOOST_ALL_DYN_LINK if they want all boost
  146. // libraries to be dynamically linked, or BOOST_WHATEVER_DYN_LINK
  147. // if they want just this one to be dynamically liked:
  148. #if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_WHATEVER_DYN_LINK)
  149. // export if this is our own source, otherwise import:
  150. #ifdef BOOST_WHATEVER_SOURCE
  151. # define BOOST_WHATEVER_DECL __declspec(dllexport)
  152. #else
  153. # define BOOST_WHATEVER_DECL __declspec(dllimport)
  154. #endif // BOOST_WHATEVER_SOURCE
  155. #endif // DYN_LINK
  156. #endif // BOOST_HAS_DECLSPEC
  157. //
  158. // if BOOST_WHATEVER_DECL isn't defined yet define it now:
  159. #ifndef BOOST_WHATEVER_DECL
  160. #define BOOST_WHATEVER_DECL
  161. #endif
  162. //
  163. // this header declares one class, and one function by way of examples:
  164. //
  165. class BOOST_WHATEVER_DECL whatever
  166. {
  167. // details.
  168. };
  169. BOOST_WHATEVER_DECL whatever get_whatever();
  170. #endif
  171. </pre>
  172. And then in the source code for this library one would use:
  173. <pre>
  174. //
  175. // define BOOST_WHATEVER SOURCE so that our library's
  176. // setup code knows that we are building the library (possibly exporting code),
  177. // rather than using it (possibly importing code):
  178. //
  179. #define BOOST_WHATEVER_SOURCE
  180. #include &lt;boost/whatever.hpp&gt;
  181. // class members don't need any further annotation:
  182. whatever::whatever() { }
  183. // but functions do:
  184. BOOST_WHATEVER_DECL whatever get_whatever()
  185. {
  186. return whatever();
  187. }
  188. </pre>
  189. <H4>Importing/exporting dependencies</H4>
  190. <P>As well as exporting your main classes and functions (those that are actually
  191. documented), Microsoft Visual C++ will warn loudly and often if you try to
  192. import/export a class whose dependencies are not also exported. Dependencies
  193. include: any base classes, any user defined types used as data members, plus
  194. all of the dependencies of your dependencies and so on. This causes particular
  195. problems when a dependency is a template class, because although it is
  196. technically possible to export these, it is not at all easy, especially if the
  197. template itself has dependencies which are implementation-specific details. In
  198. most cases it's probably better to simply suppress the warnings using:</P>
  199. <PRE>#ifdef BOOST_MSVC
  200. # pragma warning(push)
  201. # pragma warning(disable : 4251 4231 4660)
  202. #endif
  203. // code here
  204. #ifdef BOOST_MSVC
  205. #pragma warning(pop)
  206. #endif
  207. </PRE>
  208. <p>This is safe provided that there are no dependencies that are (template)
  209. classes with non-constant static data members, these really do need exporting,
  210. otherwise there will be multiple copies of the static data members in the
  211. program, and that's really really bad.
  212. </p>
  213. <p>Historical note: on 16-bit Windows you really did have to export all
  214. dependencies or the code wouldn't work, however since the latest Visual Studio
  215. .NET supports the import/export of individual member functions, it's a
  216. reasonably safe bet that Windows compilers won't do anything nasty - like
  217. changing the class's ABI - when importing/exporting a class.</p>
  218. <h4>Rationale:</h4>
  219. <p><EM>Why bother - doesn't the import/export mechanism take up more code that the
  220. classes themselves?</EM></p>
  221. <P>A good point, and probably true, however there are some circumstances where
  222. library code must be placed in a shared library - for example when the
  223. application consists of multiple dll's as well as the executable, and more than
  224. one those dll's link to the same Boost library - in this case if the library
  225. isn't dynamically linked and it contains any global data (even if that data is
  226. private to the internals of the library) then really bad things can happen -
  227. even without global data, we will still get a code bloating effect.
  228. Incidentally, for larger applications, splitting the application into multiple
  229. dll's can be highly advantageous - by using Microsoft's "delay load" feature
  230. the application will load only those parts it really needs at any one time,
  231. giving the impression of a much more responsive and faster-loading application.</P>
  232. <p><EM>Why static linking by default? </EM>
  233. </p>
  234. <P>In the worked example above, the code assumes that the library will be
  235. statically linked unless the user asks otherwise. Most users seem to prefer
  236. this (there are no separate dll's to distribute, and the overall distribution
  237. size is often significantly smaller this way as well: i.e. you pay for what you
  238. use and no more), but this is a subjective call, and some libraries may even
  239. only be available in dynamic versions (Boost.threads for example).</P>
  240. <h3><A name="auto-link"></A>Automatic Library Selection and Linking with <a href="../boost/config/auto_link.hpp">
  241. auto_link.hpp</a></h3>
  242. <p>Many Windows compilers ship with multiple runtime libraries - for example
  243. Microsoft Visual Studio .NET comes with 6 versions of the C and C++ runtime. It
  244. is essential that the Boost library that the user links to is built against the
  245. same C runtime as the program is built against. If that is not the case, then
  246. the user will experience linker errors at best, and runtime crashes at worst.
  247. The Boost build system manages this by providing different build variants, each
  248. of which is build against a different runtime, and gets a slightly different
  249. mangled name depending upon which runtime it is built against. For example the
  250. regex libraries get named as follows when built with Visual Studio .NET 2003:</p>
  251. <pre>boost_regex-vc71-mt-1_31.lib
  252. boost_regex-vc71-mt-gd-1_31.lib
  253. libboost_regex-vc71-mt-1_31.lib
  254. libboost_regex-vc71-mt-gd-1_31.lib
  255. libboost_regex-vc71-mt-s-1_31.lib
  256. libboost_regex-vc71-mt-sgd-1_31.lib
  257. libboost_regex-vc71-s-1_31.lib
  258. libboost_regex-vc71-sgd-1_31.lib
  259. </pre>
  260. <p>The difficulty now is selecting which of these the user should link his or her
  261. code to.</p>
  262. <p>In contrast, most Unix compilers typically only have one runtime (or sometimes
  263. two if there is a separate thread safe option). For these systems the only
  264. choice in selecting the right library variant is whether they want debugging
  265. info, and possibly thread safety.
  266. </p>
  267. <p>Historically Microsoft Windows compilers have managed this issue by providing a
  268. #pragma option that allows the header for a library to automatically select the
  269. library to link to. This makes everything automatic and extremely easy for the
  270. end user: as soon as they include a header file that has separate source code,
  271. the name of the right library build variant gets embedded in the object file,
  272. and as long as that library is in the linker search path, it will get pulled in
  273. by the linker without any user intervention.</p>
  274. <p>Automatic library selection and linking can be enabled for a Boost library by
  275. including the header &lt;boost/config/auto_link.hpp&gt;, after first defining
  276. BOOST_LIB_NAME and, if applicable, BOOST_DYN_LINK.</p>
  277. <pre>//
  278. // Automatically link to the correct build variant where possible.
  279. //
  280. #if !defined(BOOST_ALL_NO_LIB) &amp;&amp; !defined(BOOST_WHATEVER_NO_LIB) &amp;&amp; !defined(BOOST_WHATEVER_SOURCE)
  281. //
  282. // Set the name of our library, this will get undef'ed by auto_link.hpp
  283. // once it's done with it:
  284. //
  285. #define BOOST_LIB_NAME boost_whatever
  286. //
  287. // If we're importing code from a dll, then tell auto_link.hpp about it:
  288. //
  289. #if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_WHATEVER_DYN_LINK)
  290. # define BOOST_DYN_LINK
  291. #endif
  292. //
  293. // And include the header that does the work:
  294. //
  295. #include &lt;boost/config/auto_link.hpp&gt;
  296. #endif // auto-linking disabled
  297. </pre>
  298. <p>The library's user documentation should note that the feature can be disabled
  299. by defining either BOOST_ALL_NO_LIB or BOOST_WHATEVER_NO_LIB:</p>
  300. <P>If for any reason you need to debug this feature, the header
  301. &lt;boost/config/auto_link.hpp&gt; will output some helpful diagnostic messages
  302. if you first define BOOST_LIB_DIAGNOSTIC.</P>
  303. <H2><A name="build_changes"></A>Changes Affecting the Build System</H2>
  304. <H3><a name="build"></a><A name="jamfile"></A>Creating the library Jamfile</H3>
  305. <P>The Jamfile for building library "whatever" typically lives in
  306. boost-root/libs/whatever/build, start by defining the project root for the
  307. Jamfile:</P>
  308. <PRE>subproject libs/whatever/build ; </PRE>
  309. <P>Then add the static library build target (if supported):</P>
  310. <PRE>lib
  311. boost_whatever
  312. : # list all the sources for this
  313. library:
  314. ../src/whatever.cpp
  315. : # all build requirements go
  316. here. # the "common-variant-tag" rule ensures that the library will
  317. # be named according to the rules used by the install
  318. # and auto-link features:
  319. common-variant-tag
  320. # set include path for Boost headers:
  321. &lt;sysinclude&gt;$(BOOST_ROOT)
  322. :
  323. # list default build variants here
  324. debug release
  325. ; </PRE>
  326. <P>Then add the dll build target (if supported).&nbsp;&nbsp;In this case the build
  327. requirements section get an extra define: so that our sources know to export
  328. their own symbols (and import those from any other boost libs on which we may
  329. be dependent).&nbsp; We also restict shared library builds to dynamic-runtime
  330. build variants, if we don't do this then dll's linked against static runtimes
  331. are unlikely to function correctly (the dll will have a separate runtime from
  332. the executable using it, this generally causing problems with new and
  333. delete,&nbsp;as well as exception handling runtimes).</P>
  334. <PRE>dll
  335. boost_whatever
  336. : # list all the sources for this
  337. library:
  338. ../src/whatever.cpp
  339. : # all build requirements go
  340. here. # the "common-variant-tag" rule ensures that the library will
  341. # be named according to the rules used by the install
  342. # and auto-link features:
  343. common-variant-tag
  344. # tell our source that we're building (and maybe using) dll's:
  345. &lt;define&gt;BOOST_ALL_DYN_LINK=1
  346. # only build this for dynamic runtimes:
  347. &lt;runtime-link&gt;dynamic
  348. # set include path for Boost headers:
  349. &lt;sysinclude&gt;$(BOOST_ROOT)
  350. :
  351. # list default build variants here
  352. debug release
  353. ;
  354. </PRE>
  355. <P>Now add an install target so that Boost.Install can find this library to
  356. install:</P>
  357. <pre>install whatever lib
  358. : &lt;dll&gt;boost_whatever &lt;lib&gt;boost_whatever
  359. ;
  360. </pre>
  361. <P>Finally add a stage target that will copy the built libraries to a common
  362. sub-directory (boost-root/stage/lib):</P>
  363. <PRE>stage stage/lib : &lt;lib&gt;boost_whatever &lt;dll&gt;boost_whatever
  364. :
  365. # copy to a path rooted at BOOST_ROOT:
  366. &lt;locate&gt;$(BOOST_ROOT)
  367. # make sure the names of the libraries are correctly named:
  368. common-variant-tag
  369. # add this target to the "stage" and "all" psuedo-targets:
  370. &lt;target&gt;stage
  371. &lt;target&gt;all
  372. :
  373. debug release
  374. ;
  375. </PRE>
  376. <H3><A name="testing"></A>Testing Auto-linking</H3>
  377. <P>Testing the auto-link feature&nbsp;reasonable straightforward using
  378. the&nbsp;Boost.build system: we need to build the "whatever" library's test
  379. files without explicitly specifying the library to link to in the Jamfile, for
  380. example:</P>
  381. <PRE>subproject libs/whatever/test/auto-link-test ;
  382. # bring in the rules for testing
  383. import testing ;
  384. # start with a static linking version:
  385. run
  386. # sources
  387. ../whatever_test.cpp
  388. :
  389. : # input files
  390. : # requirements
  391. &lt;library-path&gt;../../../../stage/lib
  392. &lt;define&gt;BOOST_LIB_DIAGNOSTIC=1
  393. : # program name
  394. whatever_test
  395. ;
  396. # and then a dll linking version:
  397. run
  398. # sources
  399. ../whatever_test.cpp
  400. :
  401. : # input files
  402. : # requirements
  403. &lt;library-path&gt;../../../../stage/lib
  404. &lt;define&gt;BOOST_LIB_DIAGNOSTIC=1
  405. &lt;define&gt;BOOST_ALL_DYN_LINK=1
  406. &lt;runtime-link&gt;dynamic
  407. : # program name
  408. whatever_test_dll
  409. ;
  410. </PRE>
  411. <P>Please note however that this Jamfile will only build with compilers that do
  412. actually support auto-linking, so it should not be added to the regular
  413. regression tests.&nbsp; The Jamfile should also be built for all possible build
  414. variants, for the Microsoft / Borland compilers that means doing a:</P>
  415. <PRE>bjam -sBUILD="release debug &lt;threading&gt;multi/single &lt;runtime-link&gt;static/dynamic" test
  416. </PRE>
  417. <HR>
  418. <p><A name="copyright"></A>Revised
  419. <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
  420. 26 November, 2003<!--webbot bot="Timestamp" endspan i-checksum="39365" --></p>
  421. <p><i>© Copyright John Maddock&nbsp;1998-
  422. <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%Y" startspan --> 2003<!--webbot bot="Timestamp" endspan i-checksum="746" --></i></p>
  423. <P><I>Distributed under the Boost Software License, Version 1.0. (See
  424. accompanying file <a href="../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy
  425. at <a href=
  426. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</I></P>
  427. <P><EM>The use of code snippets from this article does not require the reproduction
  428. of this copyright notice and license declaration; if you wish to provide
  429. attribution then please provide a link to this article.</EM></P>
  430. </body>
  431. </html>
粤ICP备19079148号