C++中Boost庫安裝使用指南(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項目
- 新建項目 → Visual C++ → 控制臺應(yīng)用 → 項目名"BoostDemo"
- 解決方案資源管理器右鍵項目 → 屬性
三、項目配置###
1. C/C++ → 常規(guī) → 附加包含目錄:
C:\vcpkg\installed\x64-windows\include
2. 鏈接器 → 常規(guī) → 附加庫目錄:
C:\vcpkg\installed\x64-windows\lib
3. 鏈接器 → 輸入 → 附加依賴項:
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)錯誤: " << e.what() << std::endl;
}
}
int main() {
// 1. 文件系統(tǒng)操作
fs::path current_dir = fs::current_path();
std::cout << "當前工作目錄: " << current_dir << "\n\n";
// 創(chuàng)建測試目錄
fs::create_directories("test_dir/data");
std::ofstream("test_dir/sample.txt") << "Boost測試文件";
// 列出目錄內(nèi)容
print_directory("test_dir");
// 2. 日期時間操作
pt::ptime now = pt::second_clock::local_time();
pt::time_duration td = now.time_of_day();
std::cout << "\n當前時間: "
<< 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"
<< "相對路徑: " << p.relative_path() << "\n"
<< "父目錄: " << p.parent_path() << "\n"
<< "文件名: " << p.filename() << std::endl;
// 清理測試目錄
fs::remove_all("test_dir");
return 0;
}
注意:運行時選Release
輸出結(jié)果示例
輸出結(jié)果示例
當前工作目錄: "C:\BoostDemo\x64\Release"目錄內(nèi)容: "test_dir"
data
sample.txt當前時間: 2024-2-5 14:30:45
路徑分解演示:
根目錄: ""
相對路徑: "test_dir/data/file.dat"
父目錄: "test_dir/data"
文件名: "file.dat"
五、高級配置說明
靜態(tài)鏈接配置
# 安裝靜態(tài)庫版本 vcpkg install boost-system:x64-windows-static
項目屬性調(diào)整:
- C/C++ → 代碼生成 → 運行庫:/MT
到此這篇關(guān)于C++中Boost庫安裝使用指南(VS2022 + vcpkg)的文章就介紹到這了,更多相關(guān)C++ Boost庫安裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++ 數(shù)據(jù)結(jié)構(gòu)之kmp算法中的求Next()函數(shù)的算法
這篇文章主要介紹了C++ 數(shù)據(jù)結(jié)構(gòu)之kmp算法中的求Next()函數(shù)的算法的相關(guān)資料,需要的朋友可以參考下2017-06-06
C語言用函數(shù)實現(xiàn)電話簿管理系統(tǒng)
這篇文章主要為大家詳細介紹了C語言用函數(shù)實現(xiàn)電話簿管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12
虛函數(shù)表-C++多態(tài)的實現(xiàn)原理解析
這篇文章主要介紹了虛函數(shù)表-C++多態(tài)的實現(xiàn)原理,本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02

