separate_compilation.html 22 KB

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