Compile and install the boost library in Ubuntu

Source: Internet
Author: User

Environment: Ubuntu 12.04 32bit, boost 1.49

Preparations: other function libraries are used in boost. To use the functions in boost, you must first install the libraries that may be missing in the system.

Apt-get install mpi-default-dev # install the mpi Library

Apt-get install libicu-dev # UNICODE Character Set supporting regular expressions

Apt-get install python-dev # If python is needed

Apt-get install libbz2-dev # If compilation error: bzlib. h: No such file or directory appears

After the above function library is installed, You can compile the boost library. Decompress boost_000049_0.tar.bz2, get/boost_000049_0, and switch the current working directory to this folder.

./Bootstrap. sh

Generate bjam. The preceding command can have various options. For more information, see./bootstrap. sh -- help. The -- prefix parameter can be used to specify the installation path. If the -- prefix parameter is not included (recommended), the default path is/usr/local/include and/usr/local/lib, store header files and various libraries respectively. After execution, bjam is generated, and existing scripts are automatically backed up. Note: boost 1.49 will generate two files bjam and b2 in the current directory. These two files are the same. Therefore, you can use either of the two files to execute the following steps.

Using mpi; # If you need the MPI function, you need to add at the end of the/tools/build/v2/user-config.jam File

Next, use the generated bjam script to compile the source code.

./B2-a-sHAVE_ICU = 1 #-a parameter, which indicates recompilation, and-sHAVE_ICU = 1 indicates Unicode/ICU is supported.

Note: here we compile all. Of course, you can also choose to compile only a part. The option -- with-<library> is used to compile only the specified library. For example, if you enter -- with-regex, only the regex library is compiled. Boost1.49 fully compiled by the author Intel®Core™2 Duo CPU T5750 @ 2.00 GHz X 2, 2G DDR2 memory on the old machine, using the above options, it will be almost half an hour. This time is tolerable. All compilation and installation are psychologically comfortable. Pai_^

Some common parameters of bjam are listed as follows:

-- Build-dir = <builddir> The compiled temporary file will be placed in builddir (this is better managed and can be deleted after compilation)
-- Stagedir = <stagedir> Path for storing compiled library files. The default path is stage.
-- Build-type = complete

Compile all the versions. Otherwise, only a small part of the versions will be compiled, which is equivalent:

Variant = release, threading = multi; link = shared | static; runtime-link = shared

Variant = debug | release Determine the version to compile (Debug or Release ?)
Link = static | shared Decide whether to use static or dynamic Databases
Threading = single | multi Decide whether to use a single thread or multi-thread Library
Runtime-link = static | shared Determines whether the database is static or dynamically linked to the C/C ++ standard library.
-- With-<library> Compile only the specified database, for example, input -- with-regex to compile only the regex database.
-- Show-libraries Display the name of the library to be compiled

After the compilation is complete, install the header file and the generated library in the specified path (-- prefix ).

./B2 install

So far, if everything goes well, the installation is complete. Write a small program test, from the Boost library full Development Guide-in-depth C ++ "quasi" standard library (revision) (by Luo Jianfeng, Electronic Industry Press 2012.5)

Full Development Guide for the Boost library-go deep into the C ++ standard library hd pdf Version Download:

# Include "stdcpp. hpp"

# Include <boost/timer. hpp>

 

Using namespace boost;

 

Int main ()

{

Timer t;

Cout <"max timespan:" <t. elapsed_max ()/3600 <"h" <endl;

Cout <"min timespan:" <t. elapsed_min () <"s" <endl;

 

Cout <"now time elapsed:" <t. elapsed () <"s" <endl;

 

Return EXIT_SUCCESS;

}

Program output:

Max timespan: 0.596523 h

Min timespan: 1e-06s

Now time elapsed: 0 s

Ubuntu compilation and installation of boost and use it in eclipse C/C ++

----------------------------------- Split line -----------------------------------

I also installed boost in ubuntu, but encountered some problems:
Www.bkjia.com @ www.bkjia.com :~ /Code $ g ++-lboost_filesystem-lboost_system main. cpp
/Tmp/cclYtfBf. o: In function '_ static_initialization_and_destruction_0 (int, int )':
Main. cpp :(. text + 0x49): undefined reference to 'boost: system: generic_category ()'
Main. cpp :(. text + 0x53): undefined reference to 'boost: system: generic_category ()'
Main. cpp :(. text + 0x5d): undefined reference to 'boost: system: system_category ()'
Collect2: ld returned 1 exit status

That is to say, an error occurs immediately after the # include <boost/filesystem. hpp> clause is added. I wonder if you have encountered this problem?

A:

"Boost/filesystem. hpp "this file depends on boost_system. during compilation, the-lboost_system option is required. However, this option is already specified in your compilation options. Why is there a link error. I also wondered for a long time. Later I found that the sequence of the-l option was a problem. In the man help information of g ++, I found the following:

ReferenceIt makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. thus, foo. o-lz bar. o searches library z after file foo. o but before bar. o. if bar. o refers to functions in z, those functions may not be loaded.

After reading this section, you should understand the cause of the error. Therefore, the solution is to compile with the following command:

ReferenceG ++ main. cpp-lboost_system-lboost_filesystem

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.