- 笔者为了方便地在vs中使用c++方便地进行文件/文件夹操作,需要安装使用boost::filesystem; 百度经验“怎样在VS2013中安装配置boost_1_55_0库”介绍非常详细,笔者之前在vs2013(x86)按照说明进行安装时一次成功,但是在vs2015(x64)环境下遇到一些问题,最后得到解决,现在遇到问题及解决方法做一汇总,以方便其他人;
- 安装环境
- 系统平台(win7-x64)
- [visual studio 2015 community(社区版)](安装包下载)(https://www.microsoft.com/en-us/download/details.aspx?id=48146)
- boost::filesystem 1-61
- 安装过程
- 下载boost安装包,笔者使用boost_1_61_0;
- 解压文件包到本地目录(笔者安装目录为
C:\boost_1_61_0
,应避免目录中出现空格); 打开cmd窗口, 使用cd
命令进入到boost安装目录(cd C:\boost_1_61_0
需要根据实际情况改变安装目录),进而运行文件夹下的bootstrap.bat
文件; - 命令运行完成后,会在boost安装目录下出现
bjam.exe
文件; - 在命令窗口中输入
bjam.exe
;此过程中将编译lib文件和头文件; - 在工程中添加包含目录:进入Property Manager(属性管理器)->Property(属性)->Common Properties(通用属性)->C/C++ ->General(常规)->Additional Include Directories(附加包含目录)-> 添加boost文件夹目录(我添加的是C:\boost_1_61_0);
- 在工程中添加库目录:进入Property Manager(属性管理器)->Property(属性)->Common Properties(通用属性)->Linker(链接器) ->General(常规)->Additional Library Directories(附加库目录)-> 添加boost文件夹库目录(我添加的是C:\boost_1_61_0\stage\lib);
- 验证程序:在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;