其实Linux下的编译安装过程和Window下的是差不多的(Windows下Boot的编译和使用) 首先在官网下载安装包进行解压,然后执行bootstrap.sh脚本。
tar -xzvf boost_1_62_0.tar.gz
cd boost_1_62_0
./bootstrap.sh
然后进行编译安装,默认安装路径是/usr/local。所以,相应的头文件在/usr/local/include/boost目录中,库文件在/usr/local/lib目录中。
sudo ./b2 install
最后,添加lib库自动搜索路径到/etc/ld.so.conf,然后ldconfig使设置生效。
具体操作为:运行命令sudo vim /etc/ld.so.conf
,将”include /usr/local/lib”这句话添加进去,并保存退出vim编辑器。再运行ldconfig命令使之生效。
下面是书写代码进行编译。
还是上次的代码,编译命令如下:g++ file_utiles.cpp --std=c++11 -I/usr/local/include -L/usr/local/lib -lboost_system -lboost_filesystem -o file_utiles
#include <iostream>
#include <string>
#include "boost/filesystem.hpp"
using namespace boost;
using std::cout;
using std::string;
int main()
{
string fileName("./readme.txt");
auto filePath = filesystem::path(fileName);
if (filesystem::exists(filePath)) // does path p actually exist?
{
if (filesystem::is_regular_file(filePath)) // is path p a regular file?
cout << filePath << " size is " << filesystem::file_size(filePath) << '\n';
else if (filesystem::is_directory(filePath)) // is path p a directory?
cout << filePath << " is a directory\n";
else
cout << filePath << " exists, but is not a regular file or directory\n";
}
else
cout << filePath << " does not exist\n";
return 0;
}