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.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)文章
C語(yǔ)言實(shí)現(xiàn)猜數(shù)字小游戲的示例代碼
猜數(shù)字小游戲是我們小時(shí)候喜歡我們一個(gè)經(jīng)典小游戲。本文將用C語(yǔ)言實(shí)現(xiàn)這一經(jīng)典游戲,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-08-08
C++ 數(shù)據(jù)結(jié)構(gòu)之kmp算法中的求Next()函數(shù)的算法
這篇文章主要介紹了C++ 數(shù)據(jù)結(jié)構(gòu)之kmp算法中的求Next()函數(shù)的算法的相關(guān)資料,需要的朋友可以參考下2017-06-06
C語(yǔ)言用函數(shù)實(shí)現(xiàn)電話簿管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言用函數(shù)實(shí)現(xiàn)電話簿管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
虛函數(shù)表-C++多態(tài)的實(shí)現(xiàn)原理解析
這篇文章主要介紹了虛函數(shù)表-C++多態(tài)的實(shí)現(xiàn)原理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02

