C++中Boost庫(kù)安裝使用指南(VS2022?+?vcpkg)
一、安裝Boost組件
# 管理員權(quán)限打開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.libboost_filesystem-vc143-mt-x64-1_86.lib
注意:具體名稱以自己安裝的版本與路徑為主。
四、完整示例代碼
#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)文章
VS報(bào)錯(cuò)C6011的問(wèn)題:取消對(duì)NULL指針的引用(解決方法)
這篇文章主要介紹了VS報(bào)錯(cuò)C6011的問(wèn)題:取消對(duì)NULL指針的引用(解決方法),C6011:取消對(duì)NULL指針的引用,發(fā)現(xiàn)是沒(méi)有進(jìn)行空指針的判斷,解決方案跟隨小編一起看看吧2024-01-01
C++ 中的 mutable關(guān)鍵字作用與使用場(chǎng)景分析(最新推薦)
C++中的mutable關(guān)鍵字允許在常量成員函數(shù)中修改特定成員變量,主要用于緩存機(jī)制、延遲計(jì)算和多線程同步等場(chǎng)景,它在設(shè)計(jì)中提供靈活性,但使用時(shí)需謹(jǐn)慎,本文介紹C++ 中的 mutable關(guān)鍵字作用與使用場(chǎng)景分析,感興趣的朋友一起看看吧2025-02-02
C語(yǔ)言編程中從密碼文件獲取數(shù)據(jù)的函數(shù)總結(jié)
這篇文章主要介紹了C語(yǔ)言編程中從密碼文件獲取數(shù)據(jù)的函數(shù)總結(jié),包括getpw()函數(shù)和getpwnam()函數(shù)以及getpwuid()函數(shù),需要的朋友可以參考下2015-08-08
C++面向?qū)ο笾鄳B(tài)的實(shí)現(xiàn)和應(yīng)用詳解
相信大家都知道面向?qū)ο蟮娜筇匦允欠庋b,繼承和多態(tài),下面這篇文章主要給大家介紹了關(guān)于C++面向?qū)ο笾鄳B(tài)的實(shí)現(xiàn)和應(yīng)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09
C++ 中的虛函數(shù)表及虛函數(shù)執(zhí)行原理詳解
這篇文章主要介紹了C++ 中的虛函數(shù)表及虛函數(shù)執(zhí)行原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

