How to install and use boost on Linux

Source: Internet
Author: User
Unzip 2. boost distribution boost_1_4...

 

How to install and use boost on Linux

Get boost

Boost distribution

Only the Library of the header file is required

Use boost to create a simple program

Prepare to use the boost binary file library

Link your program to the boost Library

1. get boost

Download boost_000046_1.tar.bz2

Extract

2. boost distribution

Boost_1_46_1 ...... boost root directory

Boost /..................................... all boost header files

Libs /...................................... .. tests ,. library files such as cpps and docs

Note:

(1) in the boost root directory (usually/usr/local/boost_000046_1 ),

(2) if the boost library is used during program compilation, you need to specify the header file path-I $ BOOST_ROOT

(3) because all header files are in the boost folder and the header files are hpp suffixes, the # include format is as follows:

# Include

3. only the library of the header file is required.

The vast majority of boost libraries are header-noly: they are completely composed of header files containing templates and inline functions. they do not need to be compiled separately or binary library files, nor need to be linked.

Only the following boost libraries must be separately built:

Boost. Filesystem

Boost. GraphParallel

Boost. IOStreams

Boost. MPI

Boost. ProgramOptions

Boost. Python (see the Boost. Python build documentation before building and installing it)

Boost. Regex

Boost. Serialization

Boost. Signals

Boost. System

Boost. Thread

Boost. Wave

The following standalone built is optional (optional:

Boost. DateTime Boost. Graph Boost. Math Boost. Random Boost. Test

4. use boost to create a simple program

The following program (example. cc) only uses the header-only library. It reads a string of integers from the standard input, and uses Boost. Lambda to multiply each number by 3 and then output.

View sourceprint?

# Include

# Include

# Include

# Include

 

Int main ()

{

Using namespace boost: lambda;

Typedef std: istream_iterator In;

 

Std: for_each (

In (std: cin), in (), std: cout <(_ 1*3) <"");

}

Compile: g ++-I $ BOOST_ROOT example. cc-o example

Run: echo 1 2 3 |./example

5. prepare to use the boost binary library

If your program uses a boost library that requires independent compilation, you need to first obtain these binary library files.

5.1 compile and install all binary library files.

Cd $ BOOST_ROOT

./Bootstrap. sh -- help

./Bootstrap. sh -- prefix =/usr/local # By default, the value of prefix is/usr/local.

The bjam executable file is generated, which is used to compile the boost library.

./Bjam install

5.2 only install the specified binary library file

The following uses the default system compiler, namely, gcc on Linux.

5.2.1 install Boost. build

Boost. Build is a text-based system for software development, testing, and installation. Install Boost. Build:

(1) cd $ BOOST_ROOT/tools/build/v2

(2)./bootstrap. sh

(3)./bjam install -- prefix =/usr/local/# prefix is the Boost. Build installation location

(4) put prefix/bin in PATH # of course/usr/local/bin is already in PATH

5.2.2 when bjam is called, the default compiler is used if toolset is not specified. If your Linux has a different version of gcc installed, you can use the toolset option to specify the version: toolset = gcc-4.4

5.2.3 specify the build path and use the -- build-dir =/path option. if this option is not specified, the bin. v2 folder is created under the current path by default and the generated files are placed in it.

5.2.4 call bjam

Cd $ BOOST_ROOT

Bjam -- build-dir =./build-boost toolset = gcc stage

The above Command creates static and shared non-debug multi-threaded variants of the libraries. to create all variants, use the "-- build-type = complete" option.

All boost binaries are stored in stage/lib/. if you want to specify another path, use the "-- stagedir = directory" option.

Note: to save the build time, you may need to build fewer library files:

View the library file name -- show-libraries

Restrict which libraries to build -- with-libraryname or -- without-libraryname

Select a specific build variant adding release or debug to the command line

Friendly reminder: Boost. Build generates a lot of report output. if you can ensure that there are no errors during the creation process, you can disable these outputs to save time. Method: append "> build. log 2> & 1" after the command"

6. link your program to the boost Library

The following program extracts the "topic" content from the email. it uses the Boost. Regex library, which needs to be compiled separately.

View sourceprint?

# Include

# Include

# Include

 

Int main ()

{

Std: string line;

Boost: regex pat ("^ Subject: (Re: | Aw :)*(.*)");

    

While (std: cin)

{

Std: getline (std: cin, line );

Boost: smatch matches;

If (boost: regex_match (line, matches, pat ))

Std: cout <matches [2] <std: endl;

}

}

Compile:

Method A: g ++-I $ BOOST_ROOT example. cc-o example $ BOOST_ROOT/stage/lib/-lboost_regex.a

Method B: g ++-I $ BOOST_ROOT example. cc-o example-L $ BOOST_ROOT/stage/lib/-lboost_regex

When you want to use multiple libraries from one path, using method B is effort-saving (paid off ). Note that Method B is not specified. a (static database) is still in progress. so (dynamic library), the system will automatically help you choose to use static library or dynamic library, of course, you can use the option "-static" to explicitly specify.

6.1 Naming of library files

Take libboost_regex-gcc34-mt-d-1_36 for example:

Lib ................................ generic prefix

Boost_regex...

The toolset used during compilation is a gcc-3.4.

Mt ................................... multithreading is supported during compilation.

D/s/g/y/p...

When 36 ........................... Tag version

6.2 run our program

Create a new mail.txt file first

View sourceprint?

To: George Shmidlap

From: Rita Marlowe

Subject: Will Success Spoil Rock Hunter?

---

See subject.

If our program is linked to a shared dynamic library, we need to let the system know where to load it. Please refer to my ~ /. Bashrc file:

View sourceprint?

# Boost

Export BOOST_ROOT = "/usr/local/boost_000046_1"

Export LD_LIBRARY_PATH = "/usr/local/boost_000046_1/stage/lib: $ LD_LIBRARY_PATH"

Run the program:./example <mail.txt

Output

View sourceprint?

Will Success Spoil Rock Hunter?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.