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

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_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

Window系统

我们在使用BOOST的时候,如果需要链接一些库,是不用我们手动去链接的,归根结底还是boost的auto_link这个机制,在boost下config目录的auto_link.hpp这个文件里面,基本可以看出要根据什么宏定义去控制boost去链接什么库,比如lib开头的库编译出来的lib库,如果没有lib开头的,则是动态库,安装到客户机上的时候,我们需要带上对应的dll。

//
// select linkage opt:
//
#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

从这里我们可以看到只要不定义_DLL或者BOOST_DYN_LINK,那么boost就会自动去选择静态库。

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")

不过这里有一个比较蛋疼的地方,因为只要你include了一个boost库的头文件后,比如python,那么boost就会去自动触发链接,具体看如下代码

#ifdef BOOST_AUTO_LINK_TAGGED
#  pragma comment(lib, BOOST_LIB_PREFIX BOOST_STRINGIZE(BOOST_LIB_NAME) BOOST_LIB_THREAD_OPT BOOST_LIB_RT_OPT ".lib")
#  ifdef BOOST_LIB_DIAGNOSTIC
#     pragma message ("Linking to lib file: " BOOST_LIB_PREFIX BOOST_STRINGIZE(BOOST_LIB_NAME) BOOST_LIB_THREAD_OPT BOOST_LIB_RT_OPT ".lib")
#  endif
#elif defined(BOOST_AUTO_LINK_NOMANGLE)
#  pragma comment(lib, BOOST_STRINGIZE(BOOST_LIB_NAME) ".lib")
#  ifdef BOOST_LIB_DIAGNOSTIC
#     pragma message ("Linking to lib file: " BOOST_STRINGIZE(BOOST_LIB_NAME) ".lib")
#  endif
#else

#  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")
#  ifdef BOOST_LIB_DIAGNOSTIC
#     pragma message ("Linking to lib file: " BOOST_LIB_PREFIX BOOST_STRINGIZE(BOOST_LIB_NAME) "-" BOOST_LIB_TOOLSET BOOST_LIB_THREAD_OPT BOOST_LIB_RT_OPT "-" BOOST_LIB_VERSION ".lib")
#  endif
#endif

 所以开发的时候我们经常会遇见这种情况,命名一个项目已经链接了lib python库(Boost::Python库是Python和C++相互交互的框架,它是对Python/C API的包装,可以在Python中调用C++的类和方法,也可以让C++调用Python的类和方法。Python是一个动态类型的语言,C++是一个静态类型的语言,对于Python中的变量类型,Boost.Python都有相应的类对应。),但是他却还是去链接python的dll库,这个是因为你在一个项目中指定要链接python的lib库,而因为C++的include的机制,只要你在另外一个项目递归include的文件里面有包含python的头文件,那么 就会触发这个自动链接机制,但是却没有在另外一个项目也指定链接静态库,就会让另外一个项目跑去链接DLL,所以我们需要定义这两个宏定义BOOST_PYTHON_STATIC_MODULE和BOOST_PYTHON_STATIC_LIB。

关闭自动连接

可以使用编译宏BOOST_ALL_NO_LIB关闭自动连接,如cmake中:

# 添加预编译指令,关闭自动链接
add_compile_options(-DBOOST_ALL_NO_LIB)

linux系统

linux系统中安装了boost库,以Boost::program-options为例:

sudo apt install libboost-program-options-dev

它会自动安装依赖,如:libboost-program-options1.71-dev、libboost-program-options1.71.0、libboost1.71-dev等。

find_package(Boost REQUIRED
             COMPONENTS program_options filesystem)

但还是需要我们手动去连接boost库 

target_link_libraries(xxxxxx PRIVATE
    Boost::program_options
    Boost::filesystem

为什么不是默认的自动链接呢?我们可以看下面内容:

在find_package查找boost库时,会去cmake的目录下找boost_program_options-config.cmake文件,根据里面的配置查找Boost::program-options库。

在boost_program_options-config.cmake文件中有关闭自动连接操作:

参考:

https://www.cnblogs.com/linyilong3/p/4171503.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值