Boost库解密——自动链接库(auto_link)

2 篇文章 1 订阅

Boost库的自动链接库

boost是一个著名而强大的C++开源库,它可以说是标准库STL的补充,被称为C++的“准标准库”。
在boost库的应用中,大部分的接口只需要包含头文件即可,少部分需要链接已编译的boost库文件。然而实际使用你会发现,其实并不需要手动链接库文件,我们只需包含库文件路径,boost会帮我们自动链接库文件。
这就是boost的自动链接库——auto_link。

auto_link解析

官方说明

auto_link包含在boost/config/auto_link.hpp文件里面,打开你会发现其中的奥秘。

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库文件。
* 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)。

自动链接库

boost默认链接静态库,除非定义BOOST_DYN_LINK宏.

#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会通过各种编译器宏、默认宏,推断出完整的库文件名,然后在不同编译环境下链接需要链接的库。如:

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

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值