boost库生成文件命名和编译(转载)
boost库生成文件命名和编译
Boost库官网:http://www.boost.org/
Boost库各个版本下载地址:https://sourceforge.net/projects/boost/files/boost-binaries/
(原址:http://www.cppblog.com/Robertxiao/archive/2013/01/06/197022.html)
BOOST_LIB_PREFIX + BOOST_LIB_NAME + "-" + BOOST_LIB_TOOLSET + "-" + BOOST_LIB_THREAD_OPT + "-" + BOOST_LIB_RT_OPT + "-" + BOOST_LIB_VERSION
这些定义为:
BOOST_LIB_PREFIX: 静态库为 "lib" (否则无,是用动态链接库)
BOOST_LIB_NAME: 库的基本名称 ( 比方说 boost_regex).
BOOST_LIB_TOOLSET: 编译工具集名称 ( 比如:vc6, vc7, bcb5 )
BOOST_LIB_THREAD_OPT: 多线程为 "-mt" ,否则为空
BOOST_LIB_RT_OPT: 指示使用的运行库的后缀,
组合下面的一个或者更多字符:
s 静态运行库,指的是静态链接到运行时库(不出现表示动态).
g 调试/诊断 runtime (release if not present).
d 调试版本 (不出现表示 release 版 ).
p STLPort 版本.
注:对 vc 来说,gd 总是一起出现
BOOST_LIB_VERSION: Boost 版本, Boost 版本 x.y 表示为 x_y形式.
编译:为了简化boost库的编译,boost库中带了一个用来编译的工具,名字是bjam.exe或者b2.exe.
1:运行boost下的bootstap.bat脚本就会自动生上述的两个编译工具,并且拷贝到boost目录下. 也可以进入tools/build目录下找到类似的脚本或者项目源码来编译.
2: bjam.exe的参数
Feature |
Allowed values |
Notes |
variant |
debug,release |
|
link |
shared,static |
Determines if Boost.Build creates shared or static libraries |
threading |
single,multi |
Cause the produced binaries to be thread-safe. This requires proper support in the source code itself. |
address-model |
32,64 |
Explicitly request either 32-bit or 64-bit code generation. This typically requires that your compiler is appropriately configured. Please refer to the section called “C++ Compilers” and your compiler documentation in case of problems. |
toolset |
(Depends on configuration) |
The C++ compiler to use. See the section called “C++ Compilers” for a detailed list. (Vs2008)msvc-8.0 (vs2010)msvc-10.0 |
include |
(Arbitrary string) |
Additional include paths for C and C++ compilers. |
define |
(Arbitrary string) |
Additional macro definitions for C and C++ compilers. The string should be either SYMBOL or SYMBOL=VALUE |
cxxflags |
(Arbitrary string) |
Custom options to pass to the C++ compiler. |
cflags |
(Arbitrary string) |
Custom options to pass to the C compiler. |
linkflags |
(Arbitrary string) |
Custom options to pass to the C++ linker. |
runtime-link |
shared,static |
Determines if shared or static version of C and C++ runtimes should be used. |
--build-dir=<builddir> |
编译的临时文件会放在builddir里(这样比较好管理,编译完就可以把它删除了) |
--stagedir=<stagedir> |
存放编译后库文件的路径,默认是stage |
--build-type=complete |
编译所有版本,不然只会编译一小部分版本(确切地说是相当于:variant=release, threading=multi;link=shared|static;runtime-link=shared) |
variant=debug|release |
决定编译什么版本(对应文件中的d 调试版本 不出现表示 release 版) |
link=static|shared |
决定使用静态库还是动态库。(对应文件中的BOOST_LIB_PREFIX ) |
threading=single|multi |
决定使用单线程还是多线程库。(对应文件中的BOOST_LIB_THREAD_OPT) |
runtime-link=static|shared |
决定是静态还是动态链接C/C++标准库。(对应文件中的BOOST_LIB_THREAD_OPT) |
--with-<library> |
只编译指定的库,如输入--with-regex就只编译regex库了。 |
--show-libraries |
显示需要编译的库名称 |
bjam.exe --toolset=msvc-10.0 --with-date_time runtimelink=static link=static stage
意思是要生静态库,该静态库静态链接C运行时库
生成的文件名字是:libboost_date_time-vc100-mt-sgd-1_48.lib(debug version),libboost_date_time-vc100-mt-s-1_48.lib(release version) 两个文件.
bjam.exe --toolset=msvc-10.0 --with-date_time runtimelink=shared link=static stage
意思是要生静态库,该静态库动态链接C运行时库
生成的文件名字是:libboost_date_time-vc100-mt-gd-1_48.lib(debug verion),libboost_date_time-vc100-mt-1_48.lib(release version) 两个文件.
bjam.exe --toolset=msvc-10.0 --with-date_time runtimelink=shared link=shared stage
意思是要生动态库,该动态库动态链接C运行时库
生成的文件名字是:boost_date_time-vc100-mt-gd-1_48.lib(debug version),boost_date_time-vc100-mt-1_48.lib(release version) 两个文件.
生成的dll名字是:boost_date_time-vc100-mt-gd-1_48.dll(debug version),boost_date_time-vc100-mt-1_48.dll(release version)
编译选项方面还有install等参数.
Boost库编译后命名方式
Boost官网的《Geting Started On Windows》(http://www.boost.org/doc/libs/1_38_0/more/getting_started/windows.html)提到了Boost库的命名,摘录如下:
以 libboost_regex-vc71-mt-d-1_34.lib 为例:
- lib
前缀:除了Microsoft Windows之外,每一个Boost库的名字都以此字符串开始。在Windows上,只有普通的静态库使用lib前缀;导入库和DLL不使用。 - boost_regex
库名称:所有boost库名文件以boost_开头。 - -vc71
Toolset 标记:标识了构建该库所用的toolset和版本。 - -mt
Threading 标记:标识构建该库启用了多线程支持。不支持多线程的库没有-mt。 - -d
ABI标记:编码了影响库和其他编译代码交互的细节。对于每一种特性,向标记中添加一个字母:
Key Use this library when: s 静态链接到C++标准库和编译器运行时支撑库 g 使用标准库和运行时支撑库的调试版本 y 使用Python的特殊调试构建 d 构建代码的调试版本 p 使用STLPort标准库而不是编译器提供的默认库 n 使用STLPort已被弃用的“native iostreams” - -1_34
版本标记:完整的Boost发布号,下划线代替点。例如,1.31.1版本将被标记为“-1_31_1”。 - .lib
扩展名:取决于操作系统。在大多数unix平台上,.a是静态库,.so是共享库。在Windows上,.dll表示共享库,.lib是静态或导入库。
下表是对Regex库编译后的文件名:
文件名 | 含义 | 编译使用该库的程序时应使用的编译选项 |
libboost_regex-vc90-mt-sgd-1_38.lib | 静态库,多线程,调试版本 使用静态调试版本C运行时库(LIBCMTD.LIB和LIBCPMTD.LIB) |
/MTd |
libboost_regex-vc90-mt-s-1_38.lib | 静态库,多线程 使用静态版本C运行时库(LIBCMT.LIB和LIBCPMT.LIB) |
/MT |
libboost_regex-vc90-mt-gd-1_38.lib | 静态库,多线程,调试版本 使用动态调试版本C运行时库(MSVCRTD.LIB和MSVCPRTD.LIB) |
/MDd |
libboost_regex-vc90-mt-1_38.lib | 静态库,多线程 使用动态版本C运行时库(MSVCRT.LIB和MSVCPRT.LIB) |
/MD |
boost_regex-vc90-mt-gd-1_38.lib | 导入库(boost_regex-vc90-mt-gd-1_38.dll),多线程,调试版本 | |
boost_regex-vc90-mt-1_38.lib | 导入库(boost_regex-vc90-mt-1_38.dll)多线程 |
需要注意的是,链接时,所使用的Regex库文件名必须和编译选项匹配,否则会造成如下链接错误:
LINK : warning LNK4098: defaultlib '×××××' conflicts with use of other libs; use /NODEFAULTLIB:library
原因是,当编译时,cl.exe(也就是VC的编译器)会根据上述编译选项在编译成的obj文件中植入相应的defaultlib文件名(使用DUMPBIN /DIRECTIVE ***,lib可以查看),如/MT对应的就是LIBCMT.LIB(C)和LIBCPMT.LIB(C++标准库)。当链接器处理该obj文件时,会从文件中取出该defaultlib文件名,将其放在命令行库列表的最后以供使用。对于静态库的处理也是如此,静态库也是由一些obj文件组成的,每个obj文件中也根据当时的编译选项被植入了相应的defaultlib。当链接器处理静态库时,也会将涉及到的obj文件中的defaultlib放在命令行库列表的最后。假设,我们的程序使用/MT编译,那个对应的defaultlib就是LIBCMT.LIB(C)和LIBCPMT.LIB(C++标准库)。而使用的是libboost_regex-vc90-mt-sgd-1_38.lib,它对应的defaultlib就是LIBCMTD.LIB和LIBCPMTD.LIB。链接过程中,链接器会发现采用了不同的运行时库,所以会出现上述错误。
幸运的是,Visual C++支持自动链接,当包含Regex的头文件时,Regex会根据当前工程的编译选项(不同的编译选项会定义不同的宏,具体参见上一篇C运行时库)自动告诉编译器将哪个文件送给链接器。
Boost.Regex默认使用的静态链接方式,如果希望使用动态链接方式,如何实现呢? 定义宏BOOST_REGEX_DYN_LINK。要注意,一定要在包含regex头文件之前定义该宏:
#include <boost/regex.hpp>
关于此宏的解释参见 http://www.boost.org/doc/libs/1_38_0/libs/regex/doc/html/boost_regex/configuration/linkage.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)