shamantou blog site

shamantou@gmail.com

导航

How to compile C++ Boost

摘自http://hi.baidu.com/jrckkyy/blog/item/7cc9745cc9c4c44afaf2c08e.html

首先下载boost 1.39.0.zip
然后解压文件
使用ssh传到ubuntu上
将解压好的boost库,拷贝到/opt下
使用linux的命令cp -a -r /home/server/boost /opt

然后编译boost下的jam
cd boost/tools/jam/

执行./build_dist.sh
//编译的jam文件在bin.linuxx86下

1.39.0

第一步:

./bootstrap.sh

第二步:

./bjam -install --build-dir=C:\Cpp\target toolset=gcc --build-type=complete stage

 

bjam -sTOOLS=gcc --with-asio install

 

然后使用命令编译boost库
./bjam "-sBUILD debug release" --without-python
在ubuntu下编译成功


cmake .

make

 

linux下安装boost2008年04月20日 星期日 14:23
以下是在ubuntu 7.10 (内核 2.6.22-14)下安装的例子:

一、下载最新的 boost 库,下载地址: http://www.boost.org/users/download/

二、在适当的位置解压 boost 库,推荐把 boost 库解压到 /usr/local/ 下:
$ cd dowload/     # 这个地方替换成你的boost库下载目录
$ tar xvjf boost_1_35_0.tar.bz2    # 如果是.tar.gz 的话,用 tar xvzf boost_1_35_0.tar.gz
$ sudo mv boost_1_35_0 /usr/local/

三、安装 boost,boost 库的安装应该分为两部分,第一部分是安装无需编译(HEAD ONLY)的boost库,第二部分是安装需要单独编译(separately-compiled)的boost库。这里先写如何安装HEAD ONLY库:其实就是给boost的头文件的目录加一个名字连接到系统目录里:
$ cd /usr/include
$ sudo ln -s /usr/local/boost_1_35_0/boost boost
HEAD ONLY 库安装完成。

四、安装 需要单独编译的 boost 库:
$ cd /usr/local/boost_1_35_0
配置编译全部库:
    $ sudo ./configure --with-libraries=all
你也可以指定要编译的库:
    $ sudo ./configure --with-libraries=program_options,regex
可以选的库有:date_time, filesystem    - function_types    - graph    - iostreams    - mpi    -program_options    - python    - regex    - serialization    - signals    - system    - test    - thread    - wave,库与库之间用,号分隔,不要有空格。
然后是复制所需要的.so文件到系统动态库下面:
例如,如果用到regex,则需要 libboost_regex-gcc41-mt-1_35.so.1.35.0 动态库:
    $ sudo cp /usr/local/lib/libboost_regex-gcc41-mt-1_35.so.1.35.0 /usr/lib/
完成。

注意:以后编译的时候,如果用到了需要单独编译的boost库,就在编译的时候增加下面一句:例如需要program_options库,编译选项:
g++ main.cpp -o main.o \
    -L/usr/local/lib/ -lboost_program_options-gcc41-mt
这样,gcc就会自动去找对应的 libboost------.a 文件或 .so文件了。

Done.

 


boost库编译方法,留个记号,免得忘了。

Windows下:整个过程也会自动识别你现在装的编译器,例如vc,vs2005,vs2008等

第一步:D:\boost_1_35_0\tools\jam\build_dist.bat

第二步:D:\boost_1_35_0\tools\jam\stage\build.bat

第三步:复制D:\boost_1_35_0\tools\jam\stage\bin.ntx86\bjam.exe到D:\boost_1_35_0\下

C++代码
bjam.exe --build-type=minimal --with-date_time --with-filesystem --with-program_options --with-regex --with-serialization --with-system --with-thread toolset=msvc-9.0 variant=release link=static threading=multi stage  

bjam.exe --build-type=minimal --with-date_time --with-filesystem --with-program_options --with-regex --with-serialization --with-system --with-thread toolset=msvc-9.0 variant=release link=static threading=multi stage
只生成.lib

Linux下:

C++代码
./bjam --build-type=minimal --with-date_time --with-filesystem --with-program_options --with-regex --with-serialization --with-system --with-thread variant=release link=static threading=multi stage  

./bjam --build-type=minimal --with-date_time --with-filesystem --with-program_options --with-regex --with-serialization --with-system --with-thread variant=release link=static threading=multi stage


:-)


////////////////////////////////////////////////
许多新人对于编译BOOST感到无从下手,甚至因此而放弃使用BOOST,那真的太可惜了,下面我把一些常用的BOOST编译方法贴于此,同时也作为自己的笔记。

首先下载bjam.exe,复制到 $BOOST$ 目录下。或者自己生成bjam,打开Visual Studio 2008 命令提示窗口$BOOST$\tools\jam\src,执行 build.bat 会在$BOOST$\tools\jam\src\bin.ntx86 生成 bjam.exe 文件。复制文件 bjam.exe 文件到 $BOOST$\下。

1.完全编译安装:
bjam --toolset=msvc install
完成后会生成一个bin.v2编译时的临时目录,手动删除。生成另一个目录C:\boost,里面为所有的头文件和库文件。头文件目录为boost_1_34_1\boost目录复制过去的。

2.只编译相应的库文件
bjam --toolset=msvc stage
完成后同样会生成bin.v2临时目录。另一个目录为stage文件,里面有对应的库文件。

3.查看需要编译才能使用的库列表
bjam --show-libraries

4.编译特定的库,如只编译regex
bjam --toolset=msvc --with-regex stage
生成的库文件在stage目录中。

5.不编译某个库,如不编译regex
bjam --toolset=msvc --without-regex stage
生成的库文件在stage目录中。

6.编译特定的库,如只编译regex,生成debug,多线程,共享连接版本,并保存在stage。
bjam --toolset=msvc --with-regex stage debug threading=multi link=shared

7.生成 mt-sgd 的静态库(runtime-link-static)
bjam "-sTOOLS=vc-8_0" --with-thread install debug release runtime-link=static

8.编译regex库。
bjam --toolset=msvc --with-regex stage debug release threading=multi threading=single link=shared link=static runtime-link=shared runtime-link=static

boost的安装方法:

对于DLL版本
bjam --toolset=msvc link=shared runtime-link=shared threading=multi stage debug release install

对于lib版本
bjam --toolset=msvc link=static runtime-link=shared threading=multi stage debug release install


另外,在$BOOST$\tools\build\v2\user-config.jam找到下面的地文

# -------------------
# MSVC configuration.
# -------------------

# Configure msvc (default version, searched for in standard locations and PATH).
# using msvc ;

# Configure specific msvc version (searched for in standard locations and PATH).
# using msvc : 8.0 ;

#在这里添加 vs2008 的配置

using msvc : 9.0 : : /wd4819 /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 ;

#在这里添加 vs2005 的配置

using msvc : 8.0 : : <compileflags>/wd4819 <compileflags>/D_CRT_SECURE_NO_DEPRECATE <compileflags>/D_SCL_SECURE_NO_DEPRECATE <compileflags>/D_SECURE_SCL=0 ;   

然后进入 $BOOST$ 目录,执行bjam.exe 编译命令

//下面的命令的各选项的说明:
//prefix    将boost安装到的路径(生成的头文件和库文件都会放到该路径中)。
//重定义以下变量(利用-s设置):
//VC80_ROOT   vc2005的安装路径,如果未将vc2005安装到默认位置,你必须指定该项。
//TOOLS         使用的编译工具,vc2005对应的是vc-8_0
//PYTHON_ROOT   ython的安装目录,如果未将BOOST安装到默认位置,你必须指定该项。
//BUILD         编译结果选项,默认会生成尽可能多的版本,如调试版/发行版,静态库/动态库,单线程/多线程。

bjam 命令说明

 

Boost.Build V2 (Milestone 12)
Boost.Jam 03.1.16

Project-specific help:

Project has jamfile at Jamroot

Usage:

bjam [options] [properties] [install|stage]

Builds and installs Boost.

Targets and Related Options:

install                 Install headers and compiled library files to the
=======                 configured locations (below).

--prefix=<PREFIX>       Install architecture independent files here.
                          Default; C:\Boost on Win32
                          Default; /usr/local on Unix. Linux, etc.

--exec-prefix=<EPREFIX> Install architecture dependent files here.
                          Default; <PREFIX>

--libdir=<DIR>          Install library files here.
                          Default; <EPREFIX>/lib

--includedir=<HDRDIR>   Install header files here.
                          Default; <PREFIX>/include

stage                   Build and install only compiled library files
=====                   to the stage directory.

--stagedir=<STAGEDIR>   Install library files here
                          Default; ./stage

Other Options:

--build-type=<type>     Build the specified pre-defined set of variations
                          of the libraries. Note, that which variants get
                          built depends on what each library supports.

                              minimal (default) - Builds the single
                              "release" version of the libraries. This
                              release corresponds to specifying:
                              "release <threading>multi <link>shared
                              <link>static <runtime-link>shared" as the
                              Boost.Build variant to build.

                              complete - Attempts to build all possible
                              variations.

--build-dir=DIR         Build in this location instead of building
                          within the distribution tree. Recommended!

--show-libraries        Displays the list of Boost libraries that require
                          build and installation steps, then exit.

--layout=<layout>       Determines whether to choose library names
                          and header locations such that multiple
                          versions of Boost or multiple compilers can
                          be used on the same system.

                              versioned (default) - Names of boost
                              binaries include the Boost version
                              number and the name and version of the
                              compiler. Boost headers are installed
                              in a subdirectory of <HDRDIR> whose
                              name contains the Boost version
                              number.

                              system - Binaries names do not include
                              the Boost version number or the name
                              and version number of the compiler.
                              Boost headers are installed directly
                              into <HDRDIR>. This option is
                              intended for system integrators who
                              are building distribution packages.

--buildid=ID            Adds the specified ID to the name of built
                          libraries. The default is to not add anything.

--help                  This message.

--with-<library>        Build and install the specified <library>
                          If this option is used, only libraries
                          specified using this option will be built.

--without-<library>     Do not build, stage, or install the specified
                          <library>. By default, all libraries are built.

Properties:

toolset=toolset         Indicates the toolset to build with.

variant=debug|release   Select the build variant

link=static|shared      Whether to build static or shared libraries

threading=single|multi Whether to build single or multithreaded binaries

runtime-link=static|shared    
                          Whether to link to static or shared C and C++ runtime.


Configuration help:

Configuration file at $boost$\tools\build\v2
user-config.jam

This file is used to configure your Boost.Build installation. You can modify
this file in place, or you can place it in a permanent location so that it
does not get overwritten should you get a new version of Boost.Build. See:

http://boost.org/boost-build2/doc/html/bbv2/reference.html#bbv2.reference.init

for documentation about possible permanent locations.

General command line usage:

    bjam [options] [properties] [targets]

Options, properties and targets can be specified in any order.
    
Important Options:

* --clean Remove targets instead of building
* -a Rebuild everything
* -n Don't execute the commands, only print them
* -d+2 Show commands as they are executed
* -d0 Supress all informational messages
* -q Stop at first error
* --debug-configuration Diagnose configuration
* --debug-building Report which targets are built with what properties
* --debug-generator Diagnose generator search/execution

Further Help:

The following options can be used to obtain additional documentation.

* --help-options Print more obscure command line options.
* --help-internal Boost.Build implementation details.
* --help-doc-options Implementation details doc formatting.

编译所有版本:

bjam --toolset=msvc-8.0 --prefix=$lib-and-dll-out-dir$ --build-type=complete install
等待编译完成.

设置开发环境:

打开VS2005 选择 工具->选项->vc++目录
设置包含文件目录$lib-and-dll-out-dir$\include\boost-1_37\boost
设置引用文件目录:$lib-and-dll-out-dir$\lib
完成.可以使用了.


编译安装BOOST库
从boost官方网站上下载最新的1.39版boost库。

tar -xjvf boost_1_39_0.tar.bz2
cd boost_1_39_0
./bootstrap.sh
./bjam -install

耐性等待编译安装,偶等了2个多小时。boost库的include头文件会被安装到 /usr/local/include/boost-1_39 下。库文件会被安装到 /usr/lib 目录下。

编译简单的测试程序。

#include <boost/lexical_cast.hpp>
#include <iostream>
int main()
{
using boost::lexical_cast;
int a = lexical_cast<int>("123456");
double b = lexical_cast<double>("123.456");
std::cout<<a<<std::endl;
std::cout<<b<<std::endl;
return 0;
}

编写一个简单的Makefile文件

BOOST_INCLUDE=/usr/local/include/boost-1.39
BOOST_ROOT=/usr/local/src/boost_1_39_0
BOOST_LIB=/usr/local/lib

CXX=g++

test: test.cpp
$(CXX) -o test test.cpp -I$(BOOST_INCLUDE)
clean:
rm -rf test

进行编译

make test

执行test程序可以看到输出结果.

123456
123.456
boost库安装确认通过。
————–
编译时可能需要导入的一些库引用

#使用regex
-lboost_regex-gcc
#使用thread
-lboost_thread -pthread

<< Boost学习(二)【转】I laugh >>

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最近发表

Powered By Z-Blog 1.8 Arwen Build 81206 Copyright 2006-2009 | ooplab.org | shamantou@gmail.com | 沪ICP备08011244号 | Some Rights Reserved.