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