boost::filesystem在visual studio 2015的安裝/配置方法

  1. 筆者爲了方便地在vs中使用c++方便地進行文件/文件夾操作,需要安裝使用boost::filesystem; 百度經驗“怎樣在VS2013中安裝配置boost_1_55_0庫”介紹非常詳細,筆者之前在vs2013(x86)按照說明進行安裝時一次成功,但是在vs2015(x64)環境下遇到一些問題,最後得到解決,現在遇到問題及解決方法做一彙總,以方便其他人;
  2. 安裝環境
    1. 系統平臺(win7-x64)
    2. [visual studio 2015 community(社區版)](安裝包下載)(https://www.microsoft.com/en-us/download/details.aspx?id=48146)
    3. boost::filesystem 1-61
  3. 安裝過程
    1. 下載boost安裝包,筆者使用boost_1_61_0;
    2. 解壓文件包到本地目錄(筆者安裝目錄爲C:\boost_1_61_0,應避免目錄中出現空格); 打開cmd窗口, 使用cd命令進入到boost安裝目錄(cd C:\boost_1_61_0需要根據實際情況改變安裝目錄),進而運行文件夾下的bootstrap.bat文件;
    3. 命令運行完成後,會在boost安裝目錄下出現bjam.exe文件;
    4. 在命令窗口中輸入bjam.exe;此過程中將編譯lib文件和頭文件;
    5. 在工程中添加包含目錄:進入Property Manager(屬性管理器)->Property(屬性)->Common Properties(通用屬性)->C/C++ ->General(常規)->Additional Include Directories(附加包含目錄)-> 添加boost文件夾目錄(我添加的是C:\boost_1_61_0);
    6. 在工程中添加庫目錄:進入Property Manager(屬性管理器)->Property(屬性)->Common Properties(通用屬性)->Linker(鏈接器) ->General(常規)->Additional Library Directories(附加庫目錄)-> 添加boost文件夾庫目錄(我添加的是C:\boost_1_61_0\stage\lib);
    7. 驗證程序:在vs中新建空項目,在源文件(source files)添加cpp文件,cpp文件中複製程序;運行後,C盤中所有目錄會顯示在控制檯上(console);如果運行成功,則說明配置正常;
#include<boost\filesystem.hpp>
#include<iostream>
namespace fs = boost::filesystem;
int get_subfolders(const std::string& dir, std::vector<std::string>& filenames);
int main()
{
    const std::string dir = "C:\\";
    std::vector<std::string> filenames;
    int fileCount = get_subfolders(dir, filenames);
    std::vector<std::string >::iterator iter = filenames.begin();
    std::vector<std::string>::iterator itend = filenames.end();

    for (; iter != itend; iter++)
    {
        std::cout << (*iter) << std::endl;
    }
    std::cout << filenames.size() << std::endl;
    return  filenames.size();
}

int get_subfolders(const std::string& dir, std::vector<std::string>& filenames)
{
    fs::path path(dir);
    if (!fs::exists(path))
    {
        return -1;
    }
    fs::directory_iterator end_iter;
    for (fs::directory_iterator iter(path); iter != end_iter; ++iter)
    {
        if (fs::is_directory(iter->status()))
        {
            filenames.push_back(iter->path().string());
        }
    }
    return filenames.size();
}

4.常見問題:
1. Error-1

LINK : fatal error LNK1104: cannot open file
'libboost_filesystem-vc120-mt-sgd-1_58.lib'

;解決方案,是需要生成不同的run time check 文件;使用b2指令,輸入代碼 b2 runtime-link=static runtime-debugging=on variant=debug;參考資料3和4 ;

 2.  Error-2:

fatal error LNK1112: module machine type ‘X86’ conflicts with target
machine type ‘x64’

解決方法:需要爲編譯vs(x64)所需要的文件;具體做法是在安裝過程第四部運行bjam時候,添加相應的說明;需要注意的是代碼中需要輸入正確的vs版本號;如vs2015,那麼下述代碼中的 msvc-9.09.0應該改爲msvc-9.014.0; 具體請見參考文獻3 “64-bit version of Boost for 64-bit windows”;

bjam --toolset=msvc-9.0 address-model=64 --build-type=complete stage

5 . 參考資料
1. “How to use Boost in Visual Studio 2010” 來自 stack overflow;非常詳細地介紹了安裝過程;
2. “怎樣在VS2013中安裝配置boost_1_55_0庫 ”來自百度經驗;
3. “64-bit version of Boost for 64-bit windows”來自 stack overflow
4. “2>LINK : fatal error LNK1104: cannot open file ‘libboost_filesystem-vc120-mt-sgd-1_58.lib’”來自 stack overflow

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章