1. 程式人生 > >LINUX 環境下BOOST 庫安裝編譯

LINUX 環境下BOOST 庫安裝編譯

先以一個Header-Only Libraries為例,在linux環境下編譯。

  #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) << " " );
  } 
 編譯example.cpp
  1: g++  -I${INSTALL_DIR}/include example.cpp -o example
執行程式:
  1: echo 1 2 3 | ./example
這個例子比較簡單,只依賴boost一個頭檔案。
下面重點介紹下boost幾個庫的編譯

5. 依賴boost生成靜態庫或者動態庫

5.1 命名規則

libboost_regex-gcc34-mt-d-1_36.a
 -lboost_regex-gcc34-mt-d-1_36
BOOST_LIB_PREFIX + BOOST_LIB_NAME + "-" + BOOST_LIB_TOOLSET + "
-" + BOOST_LIB_THREAD_OPT + "-" + BOOST_LIB_RT_OPT + "-" + BOOST_LIB_VERSION 
一般的命名都是有上面的幾個部分組成.比較容易辨識。lib 
一般二進位制庫都是lib開頭
boost_regex  
Library name: all boost library filenames begin with boost_.
-gcc34
編譯工具版本
-mt
Threading tag:-mt表示多執行緒,一般不寫.
-d
Key Use this library when: Boost.Build option
s linking statically to the C++ standard library and compiler runtime support libraries. runtime-link=static
g using debug versions of the standard and runtime support libraries. runtime-debugging=on
y python-debugging=on
d building a debug version of your code.5 variant=debug
p using the STLPort standard library rather than the default one supplied with your compiler. stdlib=stlport
-1_36
Boost庫的版本1.36.lib
二進位制庫字尾,linux 靜態庫.a結尾,動態庫.so.1.36.0

5.2 編譯引數

Boost庫編譯有其自帶的編譯工具編譯bjam或者b2編譯,具體編譯選項可以有./bjam --help或者 ./b2 --help檢視.

bjam [options] [properties] [install|stage]
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 a minimal set of
                              variants. On Windows, these are static
                              multithreaded libraries in debug and release
                              modes, using shared runtime. On Linux, these are
                              static and shared multithreaded libraries in
                              release mode.

                              -- complete -- Build all possible variations.

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

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

  --layout=<layout>       Determine 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 -- Names of boost binaries include
                              the Boost version number, name and version of
                              the compiler and encoded build properties. Boost
                              headers are installed in a subdirectory of
                              <HDRDIR> whose name contains the Boost version
                              number.

                              -- tagged -- Names of boost binaries include the
                              encoded build properties such as variant and
                              threading, but do not including compiler name
                              and version, or Boost version. This option is
                              useful if you build several variants of Boost,
                              using the same compiler.

                              -- 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 building
                              distribution packages.

                          The default value is 'versioned' on Windows, and
                          'system' on Unix.

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

  --python-buildid=ID     Add the specified ID to the name of built libraries
                          that depend on Python. The default is to not add
                          anything. This ID is added in addition to --buildid.

  --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         Indicate 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.

上面的編譯選項不一一介紹了,下面編譯一個靜態庫的例子.

 ./bjam --toolset=gcc --with-regex  runtime-link=static link=static stage

最後可以檢視./stage下面的lib庫 libboost_regex.a

補充一下bjam 編譯引數:

Feature

Allowed values

Notes

variant

debug,release

link

shared,static

Determines if Boost.Build creates shared or static libraries

threading

single,multi

Cause the produced binaries to be thread-safe. This requires proper support in the source code itself.

address-model

32,64

Explicitly request either 32-bit or 64-bit code generation. This typically requires that your compiler is appropriately configured. Please refer to the section called “C++ Compilers” and your compiler documentation in case of problems.

toolset

(Depends on configuration)

(Vs2008)msvc-8.0 (vs2010)msvc-10.0

include

(Arbitrary string)

Additional include paths for C and C++ compilers.

define

(Arbitrary string)

Additional macro definitions for C and C++ compilers. The string should be either SYMBOL or SYMBOL=VALUE

cxxflags

(Arbitrary string)

Custom options to pass to the C++ compiler.

cflags

(Arbitrary string)

Custom options to pass to the C compiler.

linkflags

(Arbitrary string)

Custom options to pass to the C++ linker.

runtime-link

shared,static

Determines if shared or static version of C and C++ runtimes should be used.

--build-dir=<builddir>

編譯的臨時檔案會放在builddir裡(這樣比較好管理,編譯完就可以把它刪除了)

--stagedir=<stagedir>

存放編譯後庫檔案的路徑,預設是stage

--build-type=complete

編譯所有版本,不然只會編譯一小部分版本(確切地說是相當於:variant=release, threading=multi;link=shared|static;runtime-link=shared)

variant=debug|release

決定編譯什麼版本(對應檔案中的d 除錯版本 不出現表示 release 版)

link=static|shared

決定使用靜態庫還是動態庫。(對應檔案中的BOOST_LIB_PREFIX )

threading=single|multi

決定使用單執行緒還是多執行緒庫。(對應檔案中的BOOST_LIB_THREAD_OPT)

runtime-link=static|shared

決定是靜態還是動態連結C/C++標準庫。(對應檔案中的BOOST_LIB_THREAD_OPT)

--with-<library>

只編譯指定的庫,如輸入--with-regex就只編譯regex庫了。

--show-libraries

顯示需要編譯的庫名稱


參考文件:

boost官方文件 http://www.boost.org