getting_started.rst 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. ============================
  2. |(logo)|__ Getting Started
  3. ============================
  4. .. |(logo)| image:: ../boost.png
  5. :alt: Boost
  6. :class: boost-logo
  7. __ ../index.htm
  8. .. section-numbering::
  9. :depth: 2
  10. .. contents:: Contents
  11. :depth: 2
  12. :class: sidebar small
  13. .. ## Update this substitution for each release
  14. .. |boost_ver| replace:: ``boost_1_34_0``
  15. .. |boost_ver-bold| replace:: **boost_1_34_0**
  16. .. |root| replace:: ``/``\ *path*\ ``/``\ *to*\ ``/``\ |boost_ver|
  17. .. |winroot| replace:: *path*\ ``\``\ *to*\ ``\``\ |boost_ver|
  18. .. |winroot-default| replace:: ``C:\Program``\ `` ``\ ``Files\boost\``\ |boost_ver|
  19. .. |bold-winroot-default| replace:: **C:\\Program Files\\boost\\**\ |boost_ver-bold|
  20. Introduction
  21. ============
  22. Welcome to the Boost libraries! By the time you've completed this
  23. tutorial, you'll be at least somewhat comfortable with the contents
  24. of a Boost distribution and how to go about using it.
  25. What's Here
  26. -----------
  27. This document is designed to be an *extremely* gentle introduction,
  28. so we included a fair amount of material that may already be very
  29. familiar to you. To keep things simple, we also left out some
  30. information intermediate and advanced users will probably want. At
  31. the end of this document, we'll refer you on to resources that can
  32. help you pursue these topics further.
  33. Preliminaries
  34. -------------
  35. We use one typographic convention that might not be immediately
  36. obvious: *italic* text in examples is meant as a descriptive
  37. placeholder for something else, usually information that you'll
  38. provide. For example:
  39. .. parsed-literal::
  40. **$** echo "My name is *your name*\ "
  41. Here you're expected to imagine replacing the text “your name” with
  42. your actual name.
  43. We identify Unix and its variants such as Linux, FreeBSD, and MacOS
  44. collectively as \*nix. If you're not targeting Microsoft Windows,
  45. the instructions for \*nix users will probably work for you.
  46. Cygwin users working from the Cygwin ``bash`` prompt should also
  47. follow the \*nix instructions. To use your Cygwin compiler from
  48. the Windows command prompt, follow the instructions for Windows
  49. users.
  50. Although Boost supports a wide variety of Windows compilers
  51. (including older Microsoft compilers), most instructions for
  52. Windows users cover only the Visual Studio .NET 2003 and Visual
  53. Studio 2005. We hope that gives you enough information to adapt
  54. them for your own compiler or IDE.
  55. Get Boost
  56. =========
  57. There are basically three ways to get Boost on your system:
  58. 1. **Windows Installer**: Boost Consulting provides an installer_
  59. for Windows platforms that installs a complete Boost
  60. distribution, plus optional precompiled library binaries for
  61. Visual Studio, and (optionally) a prebuilt version of the
  62. ``bjam`` build tool.
  63. .. _Windows installer: http://www.boost-consulting.com/download.html
  64. .. |Windows installer| replace:: **Windows installer**
  65. .. _Boost Consulting: http://boost-consulting.com
  66. .. _installer: `Windows installer`_
  67. 2. **Download**: users of other platforms—and Windows
  68. users who prefer to build everything from scratch—can `download
  69. a complete Boost distribution`__ from SourceForge.
  70. .. ## Update this link for each release
  71. __ http://sourceforge.net/project/showfiles.php?group_id=7586&package_id=8041&release_id=376197
  72. - **Windows**: Download and run |boost_ver|\ ``.exe``
  73. to unpack the distribution. [#zip]_
  74. - ***nix**: Download |boost_ver|\ ``.tar.bz2``. Then, in the
  75. directory where you want to put the Boost installation,
  76. execute
  77. .. parsed-literal::
  78. tar --bzip2 -xf */path/to/*\ |boost_ver|\ .tar.bz2
  79. 3. **Boost packages** from RedHat, Debian, or some other
  80. distribution packager: these instructions may not work for you
  81. if you use 3rd party packages, because other packagers sometimes
  82. choose to break Boost up into several packages or to reorganize
  83. the directory structure of the Boost distribution. [#packagers]_
  84. The Structure of a Boost Distribution
  85. =====================================
  86. This is is a sketch of the directory structure you'll get when you
  87. unpack your Boost installation (windows users replace forward
  88. slashes with backslashes):
  89. .. parsed-literal::
  90. |boost_ver-bold|\ **/** .................\ *The “boost root directory”*
  91. **index.htm** .........\ *A copy of www.boost.org starts here*
  92. **boost/** .........................\ *All Boost Header files*
  93. **libs/** ............\ *Tests, .cpp*\ s\ *, docs, etc., by library* [#installer-src]_
  94. **index.html** ........\ *Library documentation starts here*
  95. **algorithm/**
  96. **any/**
  97. **array/**
  98. *…more libraries…*
  99. **status/** .........................\ *Boost-wide test suite*
  100. **tools/** ...........\ *Utilities, e.g. bjam, quickbook, bcp*
  101. **more/** ..........................\ *Policy documents, etc.*
  102. **doc/** ...............\ *A subset of all Boost library docs*
  103. .. sidebar:: Header Organization
  104. :class: small
  105. The organization of Boost library headers isn't entirely uniform,
  106. but most libraries follow a few patterns:
  107. * Some older libraries and most very small libraries place all
  108. public headers directly into ``boost/``.
  109. * Most libraries' public headers live in a subdirectory of
  110. ``boost/`` named after the library. For example, you'll find
  111. the Type Traits Library's ``is_void.hpp`` header in
  112. ``boost/type_traits/is_void.hpp``.
  113. * Some libraries have an “aggregate header” in ``boost/`` that
  114. ``#include``\ s all of the library's other headers. For
  115. example, Boost.Python_'s aggregate header is
  116. ``boost/python.hpp``.
  117. * Most libraries place private headers in a subdirectory called
  118. ``detail/`` or ``aux_/``. Don't look in these directories and
  119. expect to find anything you can use.
  120. A few things are worth noting right off the bat:
  121. 1. The path to the “boost root directory” is sometimes referred to
  122. as ``$BOOST_ROOT`` in documentation and mailing lists. If you
  123. used the Windows installer, that will usually be |winroot-default|.
  124. 2. To compile anything in Boost, you need a directory containing
  125. the ``boost/`` subdirectory in your ``#include`` path. For most
  126. compilers, that means adding
  127. .. parsed-literal::
  128. -I\ |root|
  129. to the command line. Specific steps for setting up ``#include``
  130. paths in Microsoft Visual Studio follow later in this document;
  131. if you use another IDE, please consult your product's
  132. documentation for instructions.
  133. 3. Since all of Boost's header files have the ``.hpp`` extension,
  134. and live in the ``boost/`` subdirectory of the boost root, your
  135. Boost ``#include`` directives will look like:
  136. .. parsed-literal::
  137. #include <boost/\ *whatever*\ .hpp>
  138. or
  139. .. parsed-literal::
  140. #include "boost/\ *whatever*\ .hpp"
  141. depending on your religion as regards the use of angle bracket
  142. includes. Even Windows users can use forward slashes in
  143. ``#include`` directives; your compiler doesn't care.
  144. 4. Don't be distracted by the ``doc/`` subdirectory; it only
  145. contains a subset of the Boost documentation. Start with
  146. ``libs/index.html`` if you're looking for the whole enchilada.
  147. Header-Only Libraries
  148. =====================
  149. The first thing many people want to know is, “how do I build
  150. Boost?” The good news is that often, there's nothing to build.
  151. .. admonition:: Nothing to Build
  152. Most Boost libraries are **header-only**: they consist *entirely
  153. of header files* containing templates and inline functions, and
  154. require no separately-compiled library binaries or special
  155. treatment when linking.
  156. .. _separate:
  157. The only Boost libraries that can't be used without separate
  158. compilation are:
  159. * Boost.Filesystem
  160. * Boost.IOStreams
  161. * Boost.ProgramOptions
  162. * Boost.Python_
  163. * Boost.Regex
  164. * Boost.Serialization
  165. * Boost.Signals
  166. * Boost.Test
  167. * Boost.Thread
  168. * Boost.Wave
  169. The DateTime library has a separately-compiled component that
  170. is only needed if you're using its to/from_string and/or
  171. serialization features or if you're targeting Visual C++ 6.x or
  172. Borland. The Graph library also has a separately-compiled part,
  173. but you won't need it unless you intend to `parse GraphViz
  174. files`__.
  175. __ ../libs/graph/doc/read_graphviz.html
  176. .. ## Keep the list of non-header-only libraries up-to-date
  177. Build a Simple Program Using Boost
  178. ==================================
  179. To keep things simple, let's start by using a header-only library.
  180. The following program reads a sequence of integers from standard
  181. input, uses Boost.Lambda to multiply each number by three, and
  182. writes them to standard output::
  183. #include <boost/lambda/lambda.hpp>
  184. #include <iostream>
  185. #include <iterator>
  186. #include <algorithm>
  187. int main()
  188. {
  189. using namespace boost::lambda;
  190. typedef std::istream_iterator<int> in;
  191. std::for_each(
  192. in(std::cin), in(), std::cout << (_1 * 3) << " " );
  193. }
  194. Copy the text of this program into a file called ``example.cpp``.
  195. .. _unix-header-only:
  196. Build on \*nix
  197. --------------
  198. In the directory where you saved ``example.cpp``, issue the
  199. following command:
  200. .. parsed-literal::
  201. c++ -I |root| example.cpp -o example
  202. To test the result, type:
  203. .. parsed-literal::
  204. echo 1 2 3 | ./example
  205. .. |next| replace:: *next...*
  206. |next|__
  207. __ `Errors and Warnings`_
  208. Build from the Visual Studio Command Prompt
  209. -------------------------------------------
  210. From your computer's *Start* menu, if you are a Visual
  211. Studio 2005 user, select
  212. *All Programs* > *Microsoft Visual Studio 2005*
  213. > *Visual Studio Tools* > *Visual Studio 2005 Command Prompt*
  214. or, if you're a Visual Studio .NET 2003 user, select
  215. *All Programs* > *Microsoft Visual Studio .NET 2003*
  216. > *Visual Studio .NET Tools* > *Visual Studio .NET 2003 Command Prompt*
  217. to bring up a special `command prompt`_ window set up for the Visual
  218. Studio compiler. In that window, type the following command and
  219. hit the return key:
  220. .. parsed-literal::
  221. cl /EHsc /I\ |winroot| *path*\ \\\ *to*\ \\example.cpp
  222. To test the result, type:
  223. .. parsed-literal::
  224. echo 1 2 3 | example
  225. |next|__
  226. __ `Errors and Warnings`_
  227. .. _vs-header-only:
  228. Build in the Visual Studio IDE
  229. ------------------------------
  230. * From Visual Studio's *File* menu, select *New* > *Project…*
  231. * In the left-hand pane of the resulting *New Project* dialog,
  232. select *Visual C++* > *Win32*.
  233. * In the right-hand pane, select *Win32 Console Application*
  234. (VS8.0) or *Win32 Console Project* (VS7.1).
  235. * In the *name* field, enter “example”
  236. * Right-click **example** in the *Solution Explorer* pane and
  237. select *Properties* from the resulting pop-up menu
  238. * In *Configuration Properties* > *C/C++* > *General* > *Additional Include
  239. Directories*, enter the path to the Boost root directory, e.g.
  240. |winroot-default|.
  241. * In *Configuration Properties* > *C/C++* > *Precompiled Headers*, change
  242. *Use Precompiled Header (/Yu)* to *Not Using Precompiled
  243. Headers*. [#pch]_
  244. * Replace the contents of the ``example.cpp`` generated by the IDE
  245. with the example code above.
  246. * From the *Build* menu, select *Build Solution*.
  247. To test your application, hit the F5 key and type the following
  248. into the resulting window, followed by the return key::
  249. 1 2 3
  250. Then hold down the control key and press "Z", followed by the
  251. return key.
  252. Errors and Warnings
  253. -------------------
  254. Don't be alarmed if you see compiler warnings from Boost headers.
  255. We try to eliminate them, but doing so isn't always practical.
  256. [#warnings]_
  257. Errors are another matter. If you're seeing compilation errors at
  258. this point in the tutorial, check to be sure you've copied the
  259. example program correctly and that you've correctly identified the
  260. Boost root directory.
  261. Get Boost Library Binaries
  262. ==========================
  263. If you want to use any of the separately-compiled Boost libraries,
  264. you'll need library binaries.
  265. Install Visual Studio Binaries
  266. ------------------------------
  267. The `Windows installer`_ supplied by Boost Consulting will download
  268. and install pre-compiled binaries into the ``lib\`` subdirectory of
  269. the boost root, typically |winroot-default|\ ``\lib\``.
  270. |next|__
  271. __ `Link Your Program to a Boost Library`_
  272. Build and Install \*nix Binaries
  273. --------------------------------
  274. Issue the following commands in the shell (don't type ``$``; it
  275. represents the shell's prompt):
  276. .. parsed-literal::
  277. **$** cd |root|
  278. **$** ./configure --help
  279. Select your configuration options and invoke ``./configure`` again.
  280. Unless you have write permission in your system's ``/usr/local/``
  281. directory, you'll probably want to at least use
  282. .. parsed-literal::
  283. **$** ./configure **--prefix=**\ *path*\ /\ *to*\ /\ *installation*\ /\ *prefix*
  284. to install somewhere else. Finally,
  285. .. parsed-literal::
  286. **$** make install
  287. which will leave Boost binaries in the ``lib/`` subdirectory of
  288. your installation prefix. You will also find a copy of the Boost
  289. headers in the ``include/`` subdirectory of the installation
  290. prefix, so you can henceforth use that directory as an ``#include``
  291. path in place of the Boost root directory.
  292. |next|__
  293. __ `Expected Build Output`_
  294. Build and Install Other Binaries
  295. --------------------------------
  296. If you're not using Visual C++ 7.1 or 8.0, or you're a \*nix user
  297. who wants want to build with a toolset other than your system's
  298. default, or if you want a nonstandard variant build of Boost
  299. (e.g. optimized, but with debug symbols), you'll need to use
  300. Boost.Build_ to create your own binaries.
  301. Boost.Build_ is a text-based system for developing, testing, and
  302. installing software. To use it, you'll need an executable called
  303. ``bjam``.
  304. .. |precompiled-bjam| replace:: pre-compiled ``bjam`` executables
  305. .. _precompiled-bjam: http://sourceforge.net/project/showfiles.php?group_id=7586&package_id=72941
  306. .. _Boost.Jam documentation: Boost.Jam_
  307. .. _Boost.Build: ../tools/build/index.html
  308. .. _Boost.Jam: ../tools/jam/index.html
  309. .. _Boost.Build documentation: Boost.Build_
  310. Get ``bjam``
  311. ............
  312. ``bjam`` is the `command-line tool`_ that drives the Boost Build
  313. system. To build Boost binaries, you'll invoke ``bjam`` from the
  314. Boost root.
  315. Boost provides |precompiled-bjam|_ for a variety of platforms.
  316. Alternatively, you can build ``bjam`` yourself using `these
  317. instructions`__.
  318. __ http://www.boost.org/doc/html/jam/building.html
  319. .. _toolset:
  320. .. _toolset-name:
  321. Identify Your Toolset
  322. .....................
  323. First, find the toolset corresponding to your compiler in the
  324. following table.
  325. +-----------+--------------------+-----------------------------+
  326. |Toolset |Vendor |Notes |
  327. |Name | | |
  328. +===========+====================+=============================+
  329. |``acc`` |Hewlett Packard |Only very recent versions are|
  330. | | |known to work well with Boost|
  331. +-----------+--------------------+-----------------------------+
  332. |``borland``|Borland | |
  333. +-----------+--------------------+-----------------------------+
  334. |``como`` |Comeau Computing |Using this toolset may |
  335. | | |require configuring__ another|
  336. | | |toolset to act as its backend|
  337. +-----------+--------------------+-----------------------------+
  338. |``cw`` |Metrowerks/FreeScale|The CodeWarrior compiler. We|
  339. | | |have not tested versions of |
  340. | | |this compiler produced since |
  341. | | |it was sold to FreeScale. |
  342. +-----------+--------------------+-----------------------------+
  343. |``dmc`` |Digital Mars |As of this Boost release, no |
  344. | | |version of dmc is known to |
  345. | | |handle Boost well. |
  346. +-----------+--------------------+-----------------------------+
  347. |``darwin`` |Apple Computer |Apple's version of the GCC |
  348. | | |toolchain with support for |
  349. | | |Darwin and MacOS X features |
  350. | | |such as frameworks. |
  351. +-----------+--------------------+-----------------------------+
  352. |``gcc`` |The Gnu Project |Includes support for Cygwin |
  353. | | |and MinGW compilers. |
  354. +-----------+--------------------+-----------------------------+
  355. |``hp_cxx`` |Hewlett Packard |Targeted at the Tru64 |
  356. | | |operating system. |
  357. +-----------+--------------------+-----------------------------+
  358. |``intel`` |Intel | |
  359. +-----------+--------------------+-----------------------------+
  360. |``kylix`` |Borland | |
  361. +-----------+--------------------+-----------------------------+
  362. |``msvc`` |Microsoft | |
  363. +-----------+--------------------+-----------------------------+
  364. |``qcc`` |QNX Software Systems| |
  365. +-----------+--------------------+-----------------------------+
  366. |``sun`` |Sun |Only very recent versions are|
  367. | | |known to work well with |
  368. | | |Boost. |
  369. +-----------+--------------------+-----------------------------+
  370. |``vacpp`` |IBM |The VisualAge C++ compiler. |
  371. +-----------+--------------------+-----------------------------+
  372. __ Boost.Build_
  373. If you have multiple versions of a particular compiler installed,
  374. you can apend the version number to the toolset name, preceded by a
  375. hyphen, e.g. ``msvc-7.1`` or ``gcc-3.4``.
  376. .. Note:: if you built ``bjam`` yourself, you may
  377. have selected a toolset name for that purpose, but that does not
  378. affect this step in any way; you still need to select a Boost.Build
  379. toolset from the table.
  380. .. _build directory:
  381. .. _build-directory:
  382. Select a Build Directory
  383. ........................
  384. Boost.Build_ will place all intermediate files it generates while
  385. building into the **build directory**. If your Boost root
  386. directory is writable, this step isn't strictly necessary: by
  387. default Boost.Build will create a ``bin.v2/`` subdirectory for that
  388. purpose in your current working directory.
  389. Invoke ``bjam``
  390. ...............
  391. .. |build-directory| replace:: *build-directory*
  392. .. |toolset-name| replace:: *toolset-name*
  393. Change your current directory to the Boost root directory and
  394. invoke ``bjam`` as follows:
  395. .. parsed-literal::
  396. bjam **--build-dir=**\ |build-directory|_ **\\**
  397. **--toolset=**\ |toolset-name|_ stage
  398. For example, on Windows, your session might look like:
  399. .. parsed-literal::
  400. C:\WINDOWS> cd |winroot-default|
  401. |winroot-default|> bjam **\\**
  402. **--build-dir=**\ %TEMP%\\build-boost **\\**
  403. **--toolset=msvc stage**
  404. And on Unix:
  405. .. parsed-literal::
  406. $ cd ~/|boost_ver|
  407. $ bjam **--build-dir=**\ ~/build-boost **--prefix=**\ ~/boost
  408. In either case, Boost.Build will place the Boost binaries in the
  409. ``stage/`` subdirectory of your `build directory`_.
  410. .. Note:: ``bjam`` is case-sensitive; it is important that all the
  411. parts shown in **bold** type above be entirely lower-case.
  412. For a description of other options you can pass when invoking
  413. ``bjam``, type::
  414. bjam --help
  415. Expected Build Output
  416. ---------------------
  417. During the process of building Boost libraries, you can expect to
  418. see some messages printed on the console. These may include
  419. * Notices about Boost library configuration—for example, the Regex
  420. library outputs a message about ICU when built without Unicode
  421. support, and the Python library may be skipped without error (but
  422. with a notice) if you don't have Python installed.
  423. * Messages from the build tool that report the number of targets
  424. that were built or skipped. Don't be surprised if those numbers
  425. don't make any sense to you; there are many targets per library.
  426. * Build action messages describing what the tool is doing, which
  427. look something like:
  428. .. parsed-literal::
  429. *toolset-name*.c++ *long*\ /\ *path*\ /\ *to*\ /\ *file*\ /\ *being*\ /\ *built*
  430. * Compiler warnings.
  431. In Case of Build Errors
  432. -----------------------
  433. The only error messages you see when building Boost—if any—should
  434. be related to the IOStreams library's support of zip and bzip2
  435. formats as described here__. Install the relevant development
  436. packages for libz and libbz2 if you need those features. Other
  437. errors when building Boost libraries are cause for concern.
  438. If it seems like the build system can't find your compiler and/or
  439. linker, consider setting up a ``user-config.jam`` file as described
  440. in the `Boost.Build documentation`_. If that isn't your problem or
  441. the ``user-config.jam`` file doesn't work for you, please address
  442. questions about configuring Boost for your compiler to the
  443. `Boost.Build mailing list`_.
  444. __ file:///home/dave/src/boost/libs/iostreams/doc/installation.html
  445. Link Your Program to a Boost Library
  446. ====================================
  447. To demonstrate linking with a Boost binary library, we'll use the
  448. following simple program that extracts the subject lines from
  449. emails. It uses the Boost.Regex_ library, which has a
  450. separately-compiled binary component. ::
  451. #include <boost/regex.hpp>
  452. #include <iostream>
  453. #include <string>
  454. int main()
  455. {
  456. std::string line;
  457. boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
  458. while (std::cin)
  459. {
  460. std::getline(std::cin, line);
  461. boost::smatch matches;
  462. if (boost::regex_match(line, matches, pat))
  463. std::cout << matches[2] << std::endl;
  464. }
  465. }
  466. .. _Boost.Regex: ../libs/regex
  467. There are two main challenges associated with linking:
  468. 1. Tool configuration, e.g. choosing command-line options or IDE
  469. build settings.
  470. 2. Identifying the library binary, among all the build variants,
  471. whose compile configuration is compatible with the rest of your
  472. project.
  473. .. Note:: Boost.Python_ users should read that library's own `build
  474. documentation`__ as there are several library-specific issues to
  475. consider.
  476. .. _Boost.Python: ../libs/python/index.html
  477. __ ../libs/python/doc/building.html
  478. Link to a Boost Library on Windows
  479. ----------------------------------
  480. .. _auto-linking:
  481. Most Windows compilers and linkers have so-called “auto-linking
  482. support,” which eliminates the second challenge. Special code in
  483. Boost header files detects your compiler options and uses that
  484. information to encode the name of the correct library into your
  485. object files; the linker selects the library with that name from
  486. the directories you've told it to search.
  487. Link to a Boost Library from the Visual Studio Command Prompt
  488. .............................................................
  489. For example, we can compile and link the above program from the
  490. Visual C++ command-line by simply adding the **bold** text below to
  491. the command line we used earlier, assuming your Boost binaries are
  492. in |winroot-default|\ ``\lib``:
  493. .. parsed-literal::
  494. cl /EHsc /I |winroot| example.cpp **\\**
  495. **/link /LIBPATH:** |bold-winroot-default|\ **\\lib**
  496. |next|__
  497. __ `Test Your Program`_
  498. Link to a Boost Library in the Visual Studio IDE
  499. ................................................
  500. Starting with the `header-only example project`__ we created
  501. earlier:
  502. __ vs-header-only_
  503. 1. Right-click **example** in the *Solution Explorer* pane and
  504. select *Properties* from the resulting pop-up menu
  505. 2. In *Configuration Properties* > *Linker* > *Additional Library
  506. Directories*, enter the path to the Boost binaries,
  507. e.g. |winroot-default|\ ``\lib\``.
  508. 3. From the *Build* menu, select *Build Solution*.
  509. |next|__
  510. __ `Test Your Program`_
  511. Link to a Boost Library On \*nix
  512. --------------------------------
  513. There are two main ways to link to libraries:
  514. A. You can specify the full path to each library:
  515. .. parsed-literal::
  516. $ c++ -I |root| example.cpp -o example **\\**
  517. **~/boost/lib/libboost_regex-gcc-3.4-mt-d-1_34.a**
  518. B. You can separately specify a directory to search (with ``-L``\
  519. *directory*) and a library name to search for (with ``-l``\
  520. *library*, [#lowercase-l]_ dropping the filename's leading ``lib`` and trailing
  521. suffix (``.a`` in this case):
  522. .. parsed-literal::
  523. $ c++ -I |root| example.cpp -o example **\\**
  524. **-L~/boost/lib/ -lboost_regex-gcc-3.4-mt-d-1_34**
  525. As you can see, this method is just as terse as method A for one
  526. library; it *really* pays off when you're using multiple
  527. libraries from the same directory. Note, however, that if you
  528. use this method with a library that has both static (``.a``) and
  529. dynamic (``.so``) builds, the system may choose one
  530. automatically for you unless you pass a special option such as
  531. ``-static`` on the command line.
  532. In both cases above, the bold text is what you'd add to `the
  533. command lines we explored earlier`__.
  534. __ unix-header-only_
  535. Library Naming
  536. --------------
  537. When auto-linking is not available, you need to know how Boost
  538. binaries are named so you can choose the right one for your build
  539. configuration. Each library filename is composed of a common
  540. sequence of elements that describe how it was built. For example,
  541. ``libboost_regex-vc71-mt-d-1_34.lib`` can be broken down into the
  542. following elements:
  543. ``lib``
  544. *Prefix*: except on Microsoft Windows, every Boost library
  545. name begins with this string. On Windows, only ordinary static
  546. libraries use the ``lib`` prefix; import libraries and DLLs do
  547. not. [#distinct]_
  548. ``boost_regex``
  549. *Library name*: all boost library filenames begin with ``boost_``.
  550. ``-vc71``
  551. *Toolset tag*: identifies the toolset and version used to build
  552. the binary.
  553. ``-mt``
  554. *Threading tag*: indicates that the library was
  555. built with multithreading support enabled. Libraries built
  556. without multithreading support can be identified by the absence
  557. of ``-mt``.
  558. ``-d``
  559. *ABI tag*: encodes details that affect the library's
  560. interoperability with other compiled code. For each such
  561. feature, a single letter is added to the tag:
  562. +-----+------------------------------------------------------------------------------+
  563. |Key |Use this library when: |
  564. +=====+==============================================================================+
  565. |``s``|linking statically to the C++ standard library and compiler runtime support |
  566. | |libraries. |
  567. +-----+------------------------------------------------------------------------------+
  568. |``g``|using debug versions of the standard and runtime support libraries. |
  569. +-----+------------------------------------------------------------------------------+
  570. |``y``|using a special `debug build of Python`__. |
  571. +-----+------------------------------------------------------------------------------+
  572. |``d``|building a debug version of your code. [#debug-abi]_ |
  573. +-----+------------------------------------------------------------------------------+
  574. |``p``|using the STLPort standard library rather than the default one supplied with |
  575. | |your compiler. |
  576. +-----+------------------------------------------------------------------------------+
  577. |``n``|using STLPort's deprecated “native iostreams” feature. [#native]_ |
  578. +-----+------------------------------------------------------------------------------+
  579. For example, if you build a debug version of your code for use
  580. with debug versions of the static runtime library and the
  581. STLPort standard library in “native iostreams” mode,
  582. the tag would be: ``-sgdpn``. If none of the above apply, the
  583. ABI tag is ommitted.
  584. ``-1_34``
  585. *Version tag*: the full Boost release number, with periods
  586. replaced by underscores. For example, version 1.31.1 would be
  587. tagged as "-1_31_1".
  588. ``.lib``
  589. *Extension*: determined according to the operating system's usual
  590. convention. On most \*nix platforms the extensions are ``.a``
  591. and ``.so`` for static libraries (archives) and shared libraries,
  592. respectively. On Windows, ``.dll`` indicates a shared library
  593. and—except for static libraries built by ``gcc`` toolset, whose
  594. names always end in ``.a``— ``.lib`` indicates a static or import
  595. library. Where supported by \*nix toolsets, a full version
  596. extension is added (e.g. ".so.1.34") and a symbolic link to the
  597. library file, named without the trailing version number, will
  598. also be created.
  599. .. _Boost.Build toolset names: toolset-name_
  600. __ ../libs/python/doc/building.html#variants
  601. Test Your Program
  602. -----------------
  603. To test our subject extraction, we'll filter the following text
  604. file. Copy it out of your browser and save it as ``jayne.txt``::
  605. To: George Shmidlap
  606. From: Rita Marlowe
  607. Subject: Will Success Spoil Rock Hunter?
  608. ---
  609. See subject.
  610. Test Your Program on Microsoft Windows
  611. ......................................
  612. In a `command prompt`_ window, type:
  613. .. parsed-literal::
  614. *path*\ \\\ *to*\ \\\ *compiled*\ \\example < *path*\ \\\ *to*\ \\\ jayne.txt
  615. The program should respond with the email subject, “Will Success
  616. Spoil Rock Hunter?”
  617. Test Your Program on \*nix
  618. ..........................
  619. If you linked to a shared library, you may need to prepare some
  620. platform-specific settings so that the system will be able to find
  621. and load it when your program is run. Most platforms have an
  622. environment variable to which you can add the directory containing
  623. the library. On many platforms (Linux, FreeBSD) that variable is
  624. ``LD_LIBRARY_PATH``, but on MacOS it's ``DYLD_LIBRARY_PATH``, and
  625. on Cygwin it's simply ``PATH``. In most shells other than ``csh``
  626. and ``tcsh``, you can adjust the variable as follows (again, don't
  627. type the ``$``\ —that represents the shell prompt):
  628. .. parsed-literal::
  629. **$** *VARIABLE_NAME*\ =\ *path/to/lib/directory*\ :${\ *VARIABLE_NAME*\ }
  630. **$** export *VARIABLE_NAME*
  631. On ``csh`` and ``tcsh``, it's
  632. .. parsed-literal::
  633. **$** setenv *VARIABLE_NAME* *path/to/lib/directory*\ :${\ *VARIABLE_NAME*\ }
  634. Once the necessary variable (if any) is set, you can run your
  635. program as follows:
  636. .. parsed-literal::
  637. **$** *path*\ /\ *to*\ /\ *compiled*\ /\ example < *path*\ /\ *to*\ /\ jayne.txt
  638. The program should respond with the email subject, “Will Success
  639. Spoil Rock Hunter?”
  640. Conclusion and Further Resources
  641. ================================
  642. This concludes your introduction to Boost and to integrating it
  643. with your programs. As you start using Boost in earnest, there are
  644. surely a few additional points you'll wish we had covered. One day
  645. we may have a “Book 2 in the Getting Started series” that addresses
  646. them. Until then, we suggest you pursue the following resources.
  647. If you can't find what you need, or there's anything we can do to
  648. make this document clearer, please post it to the `Boost Users'
  649. mailing list`_.
  650. * `Boost.Build reference manual`_
  651. * `Boost.Jam reference manual`_
  652. * `Boost Users' mailing list`_
  653. * `Boost.Build mailing list`_
  654. * `Boost.Build Wiki`_
  655. .. Admonition:: Onward
  656. .. epigraph::
  657. Good luck, and have fun!
  658. -- the Boost Developers
  659. .. _Boost.Build reference manual: ../tools/build/v2
  660. .. _Boost.Jam reference manual: `Boost.Jam`_
  661. .. _Boost Users' mailing list: mailing_lists.htm#users
  662. .. _Boost.Build Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Boost.Build_V2
  663. .. _Boost.Build mailing list: mailing_lists.htm#jamboost
  664. .. _`Using command-line tools in Windows`:
  665. .. _`command prompt`:
  666. .. _`command-line tool`:
  667. Appendix: Using command-line tools in Windows
  668. =============================================
  669. In Windows, a command-line tool is invoked by typing its name,
  670. optionally followed by arguments, into a *Command Prompt* window
  671. and pressing the Return (or Enter) key.
  672. To open *Command Prompt*, click the *Start* menu button, click
  673. *Run*, type “cmd”, and then click OK.
  674. All commands are executed within the context of a **current
  675. directory** in the filesystem. To set the current directory,
  676. type:
  677. .. parsed-literal::
  678. cd *path*\ \\\ *to*\ \\\ *some*\ \\\ *directory*
  679. followed by Return. For example,
  680. .. parsed-literal::
  681. cd |winroot-default|
  682. One way to name a directory you know about is to write
  683. .. parsed-literal::
  684. %HOMEDRIVE%%HOMEPATH%\\\ *directory-name*
  685. which indicates a sibling folder of your “My Documents” folder.
  686. Long commands can be continued across several lines by typing
  687. backslashes at the ends of all but the last line. Many of the
  688. examples on this page use that technique to save horizontal
  689. space.
  690. ------------------------------
  691. .. [#zip] If you prefer not to download executable programs, download
  692. |boost_ver|\ ``.zip`` and use an external tool to decompress
  693. it. We don't recommend using Windows' built-in decompression as
  694. it can be painfully slow for large archives.
  695. .. [#packagers] If developers of Boost packages would like to work
  696. with us to make sure these instructions can be used with their
  697. packages, we'd be glad to help. Please make your interest known
  698. to the `Boost developers' list`_.
  699. .. _Boost developers' list: mailing_lists.htm#main
  700. .. [#installer-src] If you used the `Windows installer`_ from Boost
  701. Consulting and deselected “Source and Documentation” (it's
  702. selected by default), you won't see the ``libs/`` subdirectory.
  703. That won't affect your ability to use precompiled binaries, but
  704. you won't be able to rebuild libraries from scratch.
  705. .. [#warnings] Remember that warnings are specific to each compiler
  706. implementation. The developer of a given Boost library might
  707. not have access to your compiler. Also, some warnings are
  708. extremely difficult to eliminate in generic code, to the point
  709. where it's not worth the trouble. Finally, some compilers don't
  710. have any source code mechanism for suppressing warnings.
  711. .. [#pch] There's no problem using Boost with precompiled headers;
  712. these instructions merely avoid precompiled headers because it
  713. would require Visual Studio-specific changes to the source code
  714. used in the examples.
  715. .. [#lowercase-l] That option is a dash followed by a lowercase “L”
  716. character, which looks very much like a numeral 1 in some fonts.
  717. .. [#distinct] This convention distinguishes the static version of
  718. a Boost library from the import library for an
  719. identically-configured Boost DLL, which would otherwise have the
  720. same name.
  721. .. [#debug-abi] These libraries were compiled without optimization
  722. or inlining, with full debug symbols enabled, and without
  723. ``NDEBUG`` ``#define``\ d. All though it's true that sometimes
  724. these choices don't affect binary compatibility with other
  725. compiled code, you can't count on that with Boost libraries.
  726. .. [#native] This feature of STLPort is deprecated because it's
  727. impossible to make it work transparently to the user; we don't
  728. recommend it.
粤ICP备19079148号