亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++中Boost庫(kù)安裝使用指南(VS2022?+?vcpkg)

 更新時(shí)間:2025年06月04日 09:21:10   作者:JuicyActiveGilbert  
本文主要介紹了C++中Boost庫(kù)安裝使用指南(VS2022?+?vcpkg),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前置:跨平臺(tái)C++包管理利器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)文章

最新評(píng)論