Windows平臺(tái)下boost庫(kù)的編譯和調(diào)用

視頻教程:https://www.bilibili.com/video/av97877351/

下載

編譯

  • 查看VS版本(即編譯器版本)

打開(kāi)任意VS工程:VS->Project->Properties::Genneral::Plateform Toolset(VS2015為v140)

  • 打開(kāi)VS的命令行窗口

在開(kāi)始菜單的VS菜單項(xiàng)里打開(kāi)“Developer Command Prompt for VS2015”,進(jìn)入boost目錄

  • 命令行窗口執(zhí)行bootstrap.bat

命令行運(yùn)行bootstrap.bat,會(huì)在根目錄下生產(chǎn)bjam.exe,b2.exe(bjam的升級(jí)版),project-config.jambootstrap.log四個(gè)文件

  • 命令行窗口使用bjam編譯所需的boost庫(kù)
//示例-編譯除boost::python庫(kù)之外的boost庫(kù)
bjam stage --toolset=msvc-14.0 --without-python --stagedir="E:\Learning\Boost" link=static runtime-link=shared threading=multi address-model=64

會(huì)在E:\Learning\Boost下生成一個(gè)lib目錄,目錄里是編譯好的boost的lib庫(kù)

bjam編譯參數(shù)說(shuō)明

stage/install:stage表示只生成庫(kù),install還會(huì)生成包含頭文件的include目錄,但編譯時(shí)間較長(zhǎng);默認(rèn)是stage。

--stagedir/prefix:stage時(shí)使用stagedir,install時(shí)使用prefix,表示編譯生成文件的路徑。

bjam install --toolset=msvc-14.0 --without-python --prefix="E:\Learning\Boost" link=static runtime-link=shared threading=multi address-model=64

--build-type:complete 編譯所有boost庫(kù);默認(rèn)complete。

bjam stage --toolset=msvc-14.0 --build-type=complete --stagedir="E:\Learning\Boost" link=static runtime-link=shared threading=multi address-model=64

--without/with:選擇不編譯/編譯哪些庫(kù)。默認(rèn)是全部編譯。

查看boost包含庫(kù)的命令是bjam --show-libraries

//boost::python lib
bjam stage --toolset=msvc-14.0  --with-python --stagedir="E:\Learning\Boost" link=static threading=multi address-model=64

--toolset:指定編譯器,可選的如borland、gcc、msvc-14.0(VS2025)等。

link:生成動(dòng)態(tài)鏈接庫(kù)/靜態(tài)鏈接庫(kù)。

  • static只會(huì)生成lib文件。
  • shared會(huì)生成lib文件和dll文件。
bjam stage --toolset=msvc-14.0 --build-type=complete --stagedir="E:\Learning\Boost" link=shared runtime-link=shared threading=multi address-model=64
7.png

runtime-link:動(dòng)態(tài)/靜態(tài)鏈接C/C++運(yùn)行時(shí)庫(kù)。同樣有shared和static兩種方式。

threading:?jiǎn)?多線程編譯。現(xiàn)在基本都是multi方式了。

address-model:64位平臺(tái)還是32位平臺(tái),不填就兩種平臺(tái)的庫(kù)都會(huì)編譯。

debug/release:debug版本,release版本,不填就兩種版本的庫(kù)都會(huì)編譯。

bjam stage --toolset=msvc-14.0  --with-atomic --stagedir="E:\Learning\Boost" link=static threading=multi address-model=64 debug

boost庫(kù)的命名特點(diǎn)

//link=static runtime-link=shared
libboost_atomic-vc140-mt-gd-x64-1_69.lib
libboost_atomic-vc140-mt-x64-1_69.lib
//link=static runtime-link=static
libboost_atomic-vc140-mt-sgd-x64-1_69.lib
libboost_atomic-vc140-mt-s-x64-1_69.lib
//link=shared runtime-link=shared
boost_atomic-vc140-mt-gd-x64-1_69.dll
boost_atomic-vc140-mt-gd-x64-1_69.lib
boost_atomic-vc140-mt-x64-1_69.dll
boost_atomic-vc140-mt-x64-1_69.lib

開(kāi)頭_庫(kù)名稱-編譯器版本-[多線程]-[static版本][debug版本]-平臺(tái)-版本號(hào)

  • 開(kāi)頭:以“l(fā)ib”開(kāi)頭的是“l(fā)ink=static”版本(靜態(tài)鏈接庫(kù)版本,沒(méi)有dll),而直接以“boost”開(kāi)頭的是“l(fā)ink=shared”版本(動(dòng)態(tài)鏈接庫(kù)版本,包含lib和dll)。
  • 庫(kù)名稱:其后的是boost庫(kù)名稱(比如date_time庫(kù))。
  • 編譯器的版本:與庫(kù)名稱之間以"-"而不是下劃線"_"分隔(比如 -vc140)。
  • [多線程]:有“mt”的為“threading=multi”版本,沒(méi)有該項(xiàng)則是“threading=single”版本。
  • [static版本]:有“s”的為“runtime-link=static”版本,沒(méi)有該項(xiàng)則是“runtime-link=shared”版本。
  • [debug版本]:有“gd”的為debug版本,沒(méi)有的則是release版本。
  • 平臺(tái):x64或x32
  • 版本號(hào):所有的庫(kù)都含有boost庫(kù)的版本號(hào)結(jié)尾

link參數(shù)和runtime-link的組合

link runtime-link 運(yùn)行要求 應(yīng)用工程設(shè)置
static static libboost_atomic-vc140-mt-sgd-x64-1_69.lib libboost_atomic-vc140-mt-s-x64-1_69.lib /MTd/MT
static shared libboost_atomic-vc140-mt-gd-x64-1_69.lib libboost_atomic-vc140-mt-x64-1_69.lib /MDd/MD
shared shared boost_atomic-vc140-mt-gd-x64-1_69.dll boost_atomic-vc140-mt-gd-x64-1_69.lib boost_atomic-vc140-mt-x64-1_69.dll boost_atomic-vc140-mt-x64-1_69.lib
shared static 沒(méi)有這種組合

# VS工程的使用配置

  • VS建立win32的console工程
  • 工程設(shè)置-Properties(當(dāng)前solution)
    • VC++ Directories::Include Diretorise加上boost根目錄
    • VC++ Directories::Library Diretoties加上boost編譯出來(lái)的lib目錄
  • 如果使用靜態(tài)方式連接boost::python和boost::numpy庫(kù),需要在include這兩個(gè)模塊之前加上靜態(tài)標(biāo)識(shí)
#define BOOST_PYTHON_STATIC_LIB
#define BOOST_NUMPY_STATIC_LIB
#include <boost/python.hpp>
#include <boost/python/numpy/ndarray.hpp>

示例

#include <iostream>
#include <boost/format.hpp>
#include <iomanip>


int main()
{
    std::cout << boost::format("%s:%d+%d=%d\n") % "sum" % 1 % 2 % (1 + 2);

    boost::format fmt("(%1% + %2%) * %2% = %3%\n");
    fmt % 2 % 5 %((2+5)*5);
    std::cout << fmt.str();

    fmt.parse("%|05d|\n%|-8.3|f\n%| 10S|\n%|05X|\n");
    std::cout << fmt % 62 % 2.236 % "123456789" % 48;

    fmt.clear();  
    //std::cout << fmt.str();  //error after call clear function
    std::cout << fmt % 56 % 1.125 % "987654321" % 32;

    boost::format fmtPro("%1% %2% %3% %2% %1% \n");
    std::cout << fmtPro % 1 % 2 % 3;

    fmtPro.bind_arg(2, 10);
    std::cout << fmtPro % 1 % 3;

    fmtPro.clear();
    std::cout << fmtPro % boost::io::group(std::showbase, std::oct, 111) % 333;

    fmtPro.clear_binds();
    fmtPro.modify_item(1, boost::io::group(std::hex, std::right, std::showbase, std::setw(8), std::setfill('*')));
    std::cout << fmtPro % 49 % 20 % 100;

    std::cout << 1 / 2;

    return 0;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容