C++中Boost庫(kù)安裝使用指南(VS2022?+?vcpkg)
一、安裝Boost組件
# 管理員權(quán)限打開(kāi)PowerShell cd C:\vcpkg .\vcpkg install boost-system:x64-windows boost-filesystem:x64-windows boost-date-time:x64-windows
二、創(chuàng)建VS2022項(xiàng)目
- 新建項(xiàng)目 → Visual C++ → 控制臺(tái)應(yīng)用 → 項(xiàng)目名"BoostDemo"
- 解決方案資源管理器右鍵項(xiàng)目 → 屬性
三、項(xiàng)目配置###
1. C/C++ → 常規(guī) → 附加包含目錄:
C:\vcpkg\installed\x64-windows\include
2. 鏈接器 → 常規(guī) → 附加庫(kù)目錄:
C:\vcpkg\installed\x64-windows\lib
3. 鏈接器 → 輸入 → 附加依賴項(xiàng):
boost_system-vc143-mt-x64-1_86.lib
boost_filesystem-vc143-mt-x64-1_86.lib
注意:具體名稱(chēng)以自己安裝的版本與路徑為主。
四、完整示例代碼
#include <iostream> #include <fstream> #include <boost/filesystem.hpp> #include <boost/date_time/posix_time/posix_time.hpp> namespace fs = boost::filesystem; namespace pt = boost::posix_time; void print_directory(const fs::path& dir) { try { if (fs::exists(dir)) { std::cout << "目錄內(nèi)容: " << dir << "\n"; for (const auto& entry : fs::directory_iterator(dir)) { std::cout << " " << entry.path().filename() << std::endl; } } } catch (const fs::filesystem_error& e) { std::cerr << "文件系統(tǒng)錯(cuò)誤: " << e.what() << std::endl; } } int main() { // 1. 文件系統(tǒng)操作 fs::path current_dir = fs::current_path(); std::cout << "當(dāng)前工作目錄: " << current_dir << "\n\n"; // 創(chuàng)建測(cè)試目錄 fs::create_directories("test_dir/data"); std::ofstream("test_dir/sample.txt") << "Boost測(cè)試文件"; // 列出目錄內(nèi)容 print_directory("test_dir"); // 2. 日期時(shí)間操作 pt::ptime now = pt::second_clock::local_time(); pt::time_duration td = now.time_of_day(); std::cout << "\n當(dāng)前時(shí)間: " << now.date().year() << "-" << std::setw(2) << std::setfill('0') << now.date().month().as_number() << "-" << std::setw(2) << now.date().day() << " " << td.hours() << ":" << td.minutes() << ":" << td.seconds() << std::endl; // 3. 路徑操作演示 fs::path p("test_dir/data/file.dat"); std::cout << "\n路徑分解演示:\n" << "根目錄: " << p.root_name() << "\n" << "相對(duì)路徑: " << p.relative_path() << "\n" << "父目錄: " << p.parent_path() << "\n" << "文件名: " << p.filename() << std::endl; // 清理測(cè)試目錄 fs::remove_all("test_dir"); return 0; }
注意:運(yùn)行時(shí)選Release
輸出結(jié)果示例
輸出結(jié)果示例
當(dāng)前工作目錄: "C:\BoostDemo\x64\Release"目錄內(nèi)容: "test_dir"
data
sample.txt當(dāng)前時(shí)間: 2024-2-5 14:30:45
路徑分解演示:
根目錄: ""
相對(duì)路徑: "test_dir/data/file.dat"
父目錄: "test_dir/data"
文件名: "file.dat"
五、高級(jí)配置說(shuō)明
靜態(tài)鏈接配置
# 安裝靜態(tài)庫(kù)版本 vcpkg install boost-system:x64-windows-static
項(xiàng)目屬性調(diào)整:
- C/C++ → 代碼生成 → 運(yùn)行庫(kù):/MT
到此這篇關(guān)于C++中Boost庫(kù)安裝使用指南(VS2022 + vcpkg)的文章就介紹到這了,更多相關(guān)C++ Boost庫(kù)安裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一篇文章帶你了解C語(yǔ)言:入門(mén)基礎(chǔ)(2)
這篇文章主要介紹了C語(yǔ)言入門(mén)之基礎(chǔ)知識(shí)詳解,文中有非常詳細(xì)的C語(yǔ)言使用教程及相關(guān)基礎(chǔ)知識(shí),對(duì)正在學(xué)習(xí)c語(yǔ)言的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-08-08C++中構(gòu)造函數(shù)的參數(shù)缺省的詳解
這篇文章主要介紹了C++中構(gòu)造函數(shù)的參數(shù)缺省的詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-10-10C語(yǔ)言動(dòng)態(tài)規(guī)劃之背包問(wèn)題詳解
這篇文章主要介紹了C語(yǔ)言動(dòng)態(tài)規(guī)劃之背包問(wèn)題詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04C語(yǔ)言實(shí)現(xiàn)多項(xiàng)式的相加
這篇文章主要為大家介紹了C語(yǔ)言實(shí)現(xiàn)多項(xiàng)式的相加,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10C語(yǔ)言小游戲之小熊跳板功能的實(shí)現(xiàn)
這篇文章主要介紹了C語(yǔ)言小游戲之小熊跳板功能的實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12C 語(yǔ)言中實(shí)現(xiàn)環(huán)形緩沖區(qū)
本文主要是介紹 C語(yǔ)言實(shí)現(xiàn)環(huán)形緩沖區(qū),并附有詳細(xì)實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,希望能幫助有需要的小伙伴2016-07-07