boost Getting Started on Unix Variants

http://www.boost.org/doc/libs/1_53_0/more/getting_started/unix-variants.html

1   Get Boost

The most reliable way to get a copy of Boost is to download adistribution from SourceForge:

  1. Download boost_1_53_0.tar.bz2.

  2. In the directory where you want to put the Boost installation,execute

    tar --bzip2 -xf /path/to/boost_1_53_0.tar.bz2
    

Other Packages

RedHat, Debian, and other distribution packagers supply Boostlibrary packages, however you may need to adapt theseinstructions if you use third-party packages, because theircreators usually choose to break Boost up into several packages,reorganize the directory structure of the Boost distribution,and/or rename the library binaries.1 If you haveany trouble, we suggest using an official Boost distributionfrom SourceForge.

2   The Boost Distribution

This is a sketch of the resulting directory structure:

boost_1_53_0/ .................The “boost root directory”
   index.htm .........A copy of www.boost.org starts here
   boost/ .........................All Boost Header files
    
   libs/ ............Tests, .cpps, docs, etc., by library
     index.html ........Library documentation starts here
     algorithm/
     any/
     array/
                     …more libraries…
   status/ .........................Boost-wide test suite
   tools/ ...........Utilities, e.g. Boost.Build, quickbook, bcp
   more/ ..........................Policy documents, etc.
   doc/ ...............A subset of all Boost library docs

It's important to note the following:

  1. The path to the boost root directory (often /usr/local/boost_1_53_0) issometimes referred to as $BOOST_ROOT in documentation andmailing lists .

  2. To compile anything in Boost, you need a directory containingthe boost/ subdirectory in your #include path.

  3. Since all of Boost's header files have the .hpp extension,and live in the boost/ subdirectory of the boost root, yourBoost #include directives will look like:

    #include <boost/whatever.hpp>
    

    or

    #include "boost/whatever.hpp"
    

    depending on your preference regarding the use of angle bracketincludes.

  4. Don't be distracted by the doc/ subdirectory; it onlycontains a subset of the Boost documentation. Start withlibs/index.html if you're looking for the whole enchilada.

3   Header-Only Libraries

The first thing many people want to know is, “how do I buildBoost?” The good news is that often, there's nothing to build.

Nothing to Build?

Most Boost libraries are header-only: they consist entirelyof header files containing templates and inline functions, andrequire no separately-compiled library binaries or specialtreatment when linking.

The only Boost libraries that must be built separately are:

A few libraries have optional separately-compiled binaries:

  • Boost.DateTime has a binary component that is only needed ifyou're using its to_string/from_string or serializationfeatures, or if you're targeting Visual C++ 6.x or Borland.
  • Boost.Graph also has a binary component that is only needed ifyou intend to parse GraphViz files.
  • Boost.Math has binary components for the TR1 and C99cmath functions.
  • Boost.Random has a binary component which is only needed ifyou're using random_device.
  • Boost.Test can be used in “header-only” or “separately compiled”mode, although separate compilation is recommended for serioususe.

4   Build a Simple Program Using Boost

To keep things simple, let's start by using a header-only library.The following program reads a sequence of integers from standardinput, uses Boost.Lambda to multiply each number by three, andwrites them to standard output:

#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;

    std::for_each(
        in(std::cin), in(), std::cout << (_1 * 3) << " " );
}

Copy the text of this program into a file called example.cpp.

Now, in the directory where you saved example.cpp, issue thefollowing command:

c++ -I path/to/boost_1_53_0 example.cpp -o example

To test the result, type:

echo 1 2 3 | ./example

4.1   Errors and Warnings

Don't be alarmed if you see compiler warnings originating in Boostheaders. We try to eliminate them, but doing so isn't alwayspractical.3 Errors are another matter. If you'reseeing compilation errors at this point in the tutorial, check tobe sure you've copied the example program correctly and that you'vecorrectly identified the Boost root directory.

5   Prepare to Use a Boost Library Binary

If you want to use any of the separately-compiled Boost libraries,you'll need to acquire library binaries.

5.1   Easy Build and Install

Issue the following commands in the shell (don't type $; thatrepresents the shell's prompt):

$ cd path/to/boost_1_53_0
$ ./bootstrap.sh --help

Select your configuration options and invoke ./bootstrap.sh againwithout the --help option. Unless you have write permission inyour system's /usr/local/ directory, you'll probably want to atleast use

$ ./bootstrap.sh --prefix=path/to/installation/prefix

to install somewhere else. Also, consider using the--show-libraries and --with-libraries=library-name-list options to limit thelong wait you'll experience if you build everything. Finally,

$ ./b2 install

will leave Boost binaries in the lib/ subdirectory of yourinstallation prefix. You will also find a copy of the Boostheaders in the include/ subdirectory of the installationprefix, so you can henceforth use that directory as an #includepath in place of the Boost root directory.

skip to the next step

5.2   Or, Build Custom Binaries

If you're using a compiler other than your system's default, you'llneed to use Boost.Build to create binaries.

You'll alsouse this method if you need a nonstandard build variant (see theBoost.Build documentation for more details).

Boost.CMake

There is also an experimental CMake build for boost, supported and distributedseparately. See the Boost.CMake wiki page for more information.

5.2.1   Install Boost.Build

Boost.Build is a text-based system for developing, testing, andinstalling software. First, you'll need to build andinstall it. To do this:

  1. Go to the directory tools/build/v2/.
  2. Run bootstrap.sh
  3. Run b2 install --prefix=PREFIX where PREFIX isthe directory where you want Boost.Build to be installed
  4. Add PREFIX/bin to your PATH environment variable.

5.2.2   Identify Your Toolset

First, find the toolset corresponding to your compiler in thefollowing table (an up-to-date list is always available in theBoost.Build documentation).

Note

If you previously chose a toolset for the purposes ofbuilding b2, you should assume it won't work and insteadchoose newly from the table below.

ToolsetNameVendorNotes
accHewlett PackardOnly very recent versions areknown to work well with Boost
borlandBorland 
comoComeau ComputingUsing this toolset mayrequire configuring anothertoolset to act as its backend
darwinApple ComputerApple's version of the GCCtoolchain with support forDarwin and MacOS X featuressuch as frameworks.
gccThe Gnu ProjectIncludes support for Cygwinand MinGW compilers.
hp_cxxHewlett PackardTargeted at the Tru64operating system.
intelIntel 
msvcMicrosoft 
sunSunOnly very recent versions areknown to work well withBoost.
vacppIBMThe VisualAge C++ compiler.

If you have multiple versions of a particular compiler installed,you can append the version number to the toolset name, preceded bya hyphen, e.g. intel-9.0 orborland-5.4.3.

5.2.3   Select a Build Directory

Boost.Build will place all intermediate files it generates whilebuilding into the build directory. If your Boost rootdirectory is writable, this step isn't strictly necessary: bydefault Boost.Build will create a bin.v2/ subdirectory for thatpurpose in your current working directory.

5.2.4   Invoke b2

Change your current directory to the Boost root directory andinvoke b2 as follows:

b2 --build-dir=build-directory toolset=toolset-name   stage

For a complete description of these and other invocation options,please see the Boost.Build documentation.

For example, your session might look like this:

$ cd ~/boost_1_53_0
$ b2 --build-dir=/tmp/build-boost toolset=gcc stage

That will build static and shared non-debug multi-threaded variants of the libraries. To build all variants, pass the additional option, “--build-type=complete”.

Building the special stage target places Boostlibrary binaries in the stage/lib/ subdirectory ofthe Boost tree. To use a different directory pass the--stagedir=directory option to b2.

Note

b2 is case-sensitive; it is important that all theparts shown in bold type above be entirely lower-case.

For a description of other options you can pass when invokingb2, type:

b2 --help

In particular, to limit the amount of time spent building, you maybe interested in:

  • reviewing the list of library names with --show-libraries
  • limiting which libraries get built with the --with-library-name or --without-library-name options
  • choosing a specific build variant by adding release ordebug to the command line.

Note

Boost.Build can produce a great deal of output, which canmake it easy to miss problems. If you want to make sureeverything is went well, you might redirect the output into afile by appending “>build.log 2>&1” to your command line.

5.3   Expected Build Output

During the process of building Boost libraries, you can expect tosee some messages printed on the console. These may include

  • Notices about Boost library configuration—for example, the Regexlibrary outputs a message about ICU when built without Unicodesupport, and the Python library may be skipped without error (butwith a notice) if you don't have Python installed.

  • Messages from the build tool that report the number of targetsthat were built or skipped. Don't be surprised if those numbersdon't make any sense to you; there are many targets per library.

  • Build action messages describing what the tool is doing, whichlook something like:

    toolset-name.c++ long/path/to/file/being/built
    
  • Compiler warnings.

5.4   In Case of Build Errors

The only error messages you see when building Boost—if any—shouldbe related to the IOStreams library's support of zip and bzip2formats as described here. Install the relevant developmentpackages for libz and libbz2 if you need those features. Othererrors when building Boost libraries are cause for concern.

If it seems like the build system can't find your compiler and/orlinker, consider setting up a user-config.jam file as describedhere. If that isn't your problem or the user-config.jam filedoesn't work for you, please address questions about configuring Boostfor your compiler to the Boost.Build mailing list.

7   Conclusion and Further Resources

This concludes your introduction to Boost and to integrating itwith your programs. As you start using Boost in earnest, there aresurely a few additional points you'll wish we had covered. One daywe may have a “Book 2 in the Getting Started series” that addressesthem. Until then, we suggest you pursue the following resources.If you can't find what you need, or there's anything we can do tomake this document clearer, please post it to the Boost Users'mailing list.

Onward

Good luck, and have fun!

—the Boost Developers


[1]If developers of Boost packages would like to workwith us to make sure these instructions can be used with theirpackages, we'd be glad to help. Please make your interest knownto the Boost developers' list.
[2]That option is a dash followed by a lowercase “L”character, which looks very much like a numeral 1 in some fonts.
[3]Remember that warnings are specific to each compilerimplementation. The developer of a given Boost library mightnot have access to your compiler. Also, some warnings areextremely difficult to eliminate in generic code, to the pointwhere it's not worth the trouble. Finally, some compilers don'thave any source code mechanism for suppressing warnings.
[4]This convention distinguishes the static version ofa Boost library from the import library for anidentically-configured Boost DLL, which would otherwise have thesame name.
[5]These libraries were compiled without optimizationor inlining, with full debug symbols enabled, and withoutNDEBUG #defined. Although it's true that sometimesthese choices don't affect binary compatibility with othercompiled code, you can't count on that with Boost libraries.
[6]This feature of STLPort is deprecated because it'simpossible to make it work transparently to the user; we don'trecommend it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值