一 前言
Boost是为C++语言标准库提供扩展的一些C++程序库的总称。作为扩展库,它是非常强大的,在平常项目中运用也非常广泛。
编译
首先我们要生成b2.exe,
C:\WINDOWS\system32>e:
E:\>cd E:\workspace\boost_build\boost_src\boost_1_71_0
E:\workspace\boost_build\boost_src\boost_1_71_0>bootstrap.bat
Building Boost.Build engine
Generating Boost.Build configuration in project-config.jam for msvc...
Bootstrapping is done. To build, run:
.\b2
To adjust configuration, edit 'project-config.jam'.
Further information:
- Command line help:
.\b2 --help
- Getting started guide:
http://boost.org/more/getting_started/windows.html
- Boost.Build documentation:
http://www.boost.org/build/
E:\workspace\boost_build\boost_src\boost_1_71_0>
编译32位
使用“vs2015 x86 本机工具命令提示符”
b2 stage --toolset=msvc-14.0 architecture=x86 address-model=32 --stagedir="E:\workspace\boost_build\boost_build_32" link=static runtime-link=static threading=multi debug release
编译64位
使用“vs2015 x64 本机工具命令提示符”
b2 stage --toolset=msvc-14.0 architecture=x86 address-model=64 --stagedir="E:\workspace\boost_build\boost_build_64" link=static runtime-link=static threading=multi debug release
QT中使用
加载库
在使用boost的QT项目文件.pro中添加头文件目录
INCLUDEPATH += -I E:\workspace\boost_build\boost\include
注意:大部分boost库功能的使用只需包括相应头文件即可,少数(如正则表达式库,文件系统库等)需要链接库。
使用
在.h或.cpp文件中添加头文件,并添加使用代码,如下
1.包含头文件
#include <boost/make_shared.hpp>
2.定义结构体
#define STRUCT_PTR_DECLARE(a) typedef boost::shared_ptr<struct a> a##Ptr;\
typedef boost::weak_ptr<struct a> a##WPtr
struct TestInfo
{
int x;
int y;
};
STRUCT_PTR_DECLARE(TestInfo);
3.使用智能指针
TestInfoPtr tempInfo = boost::make_shared<struct TestInfo>();
tempInfo->x = 1;
tempInfo->y = 2;
qDebug() << tempInfo->x << tempInfo->y;
结束语
这是我初步使用boost的经验,后续在使用过程中会继续完善文章,或写新文章进行分享。