Boost庫解密——自動連接庫(auto_link)

Boost庫的自動連接庫

boost是一個著名而強大的C++開源庫,它能夠說是標準庫STL的補充,被稱爲C++的「準標準庫」。
在boost庫的應用中,大部分的接口只須要包含頭文件便可,少部分須要連接已編譯的boost庫文件。然而實際使用你會發現,其實並不須要手動連接庫文件,咱們只需包含庫文件路徑,boost會幫咱們自動連接庫文件。
這就是boost的自動連接庫——auto_link。多線程

auto_link解析

官方說明

auto_link包含在boost/config/auto_link.hpp文件裏面,打開你會發現其中的奧祕。ide

USAGE:
~~~~~~

Before including this header you must define one or more of define the following macros:

BOOST_LIB_NAME:           Required: A string containing the basename of the library,
                          for example boost_regex.
BOOST_LIB_TOOLSET:        Optional: the base name of the toolset.
BOOST_DYN_LINK:           Optional: when set link to dll rather than static library.
BOOST_LIB_DIAGNOSTIC:     Optional: when set the header will print out the name
                          of the library selected (useful for debugging).
BOOST_AUTO_LINK_NOMANGLE: Specifies that we should link to BOOST_LIB_NAME.lib,
                          rather than a mangled-name version.
BOOST_AUTO_LINK_TAGGED:   Specifies that we link to libraries built with the --layout=tagged option.
                          This is essentially the same as the default name-mangled version, but without
                          the compiler name and version, or the Boost version.  Just the build options.

These macros will be undef'ed at the end of the header, further this header
has no include guards - so be sure to include it only once from your library!

Algorithm:
~~~~~~~~~~

Libraries for Borland and Microsoft compilers are automatically
selected here, the name of the lib is selected according to the following
formula:

BOOST_LIB_PREFIX
   + BOOST_LIB_NAME
   + "_"
   + BOOST_LIB_TOOLSET
   + BOOST_LIB_THREAD_OPT
   + BOOST_LIB_RT_OPT
   "-"
   + BOOST_LIB_VERSION

These are defined as:

BOOST_LIB_PREFIX:     "lib" for static libraries otherwise "".

BOOST_LIB_NAME:       The base name of the lib ( for example boost_regex).

BOOST_LIB_TOOLSET:    The compiler toolset name (vc6, vc7, bcb5 etc).

BOOST_LIB_THREAD_OPT: "-mt" for multithread builds, otherwise nothing.

BOOST_LIB_RT_OPT:     A suffix that indicates the runtime library used,
                      contains one or more of the following letters after
                      a hyphen:

                      s      static runtime (dynamic if not present).
                      g      debug/diagnostic runtime (release if not present).
                      y      Python debug/diagnostic runtime (release if not present).
                      d      debug build (release if not present).
                      p      STLport build.
                      n      STLport build without its IOStreams.

BOOST_LIB_VERSION:    The Boost version, in the form x_y, for Boost version x.y.

boost定義了各類宏,以宏來描述boost庫文件。ui

  • BOOST_LIB_PREFIX:靜態庫此宏會被定義爲"lib",動態庫爲空。
  • BOOST_LIB_NAME:庫名。
  • BOOST_LIB_TOOLSET:編譯器名。
  • BOOST_LIB_THREAD_OPT:"-mt",多線程。
  • BOOST_LIB_RT_OPT:其餘參數,其中最主要的是-s表明包含運行時庫(等同於VC編譯器的"運行庫「MT設置,不加-s表明MD),-gd表明debug。
  • BOOST_LIB_VERSION:boost庫版本。

以幾個boost庫文件爲例

boost_atomic-vc140-mt-gd-1_65_1.lib  
libboost_atomic-vc140-mt-sgd-1_65_1.lib  
libboost_atomic-vc140-mt-1_65_1.lib

第一個是atomic在vs2015調用的動態庫lib文件,第二個是atomic在vs2015調用的debug靜態庫(-mt-sgd,等同於MTd),第二個是atomic在vs2015調用的release靜態庫(-mt,等同於MD)。this

自動連接庫

boost默認連接靜態庫,除非定義BOOST_DYN_LINK宏.atom

#if (defined(_DLL) || defined(_RTLDLL)) && defined(BOOST_DYN_LINK)
#  define BOOST_LIB_PREFIX
#elif defined(BOOST_DYN_LINK)
#  error "Mixing a dll boost library with a static runtime is a really bad idea..."
#else
#  define BOOST_LIB_PREFIX "lib"
#endif

boost會經過各類編譯器宏、默認宏,推斷出完整的庫文件名,而後在不一樣編譯環境下連接須要連接的庫。如:idea

#  pragma comment(lib, BOOST_LIB_PREFIX BOOST_STRINGIZE(BOOST_LIB_NAME) "-" BOOST_LIB_TOOLSET BOOST_LIB_THREAD_OPT BOOST_LIB_RT_OPT "-" BOOST_LIB_VERSION ".lib")

注意

本篇博文基於boost1.65.1,高於或低於此版本,boost文件目錄可能會有改動。線程

相關文章
相關標籤/搜索