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文件目录可能会有改动。线程

相关文章
相关标签/搜索