一 前言
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的經驗,後續在使用過程中會繼續完善文章,或寫新文章進行分享。