boost 安装使用(linux)

1.下载
https://www.boost.org/users/download/#repository
下载最新的:
boost_1_67_0.tar.bz2
(用tar命令可以产生.tar包,gzip可以生gz包,bzip2可以产生bz2包.其中bz2的压缩率高一些。)
2. 解压
tar -xzvf boost_1_67_0.tar.gz //解压tar.gz
tar -xjvf boost_1_67_0.tar.bz2 //解压 tar.bz2
3. 编译和安装
1). cd boost_1_67_0
2). 运行bootstrap.sh脚本并设置相关参数(编译所有组件,设置gcc编译器,可以单独编译组件,也可以设置其他编译器 –with-toolset=gcc-4.8.4,
gcc -v,g++ –version):
./bootstrap.sh –with-libraries=all –with-toolset=gcc
3).编译boost
./b2 toolset=gcc
(b2貌似比bjam好)
4). 安装boost
./b2 install
boost的安装目录,默认的头文件在/usr/local/include/boost目录下,库文件在/usr/local/lib/目录下。
也可以指定安装目录。
5).刷新链接库
ldconfig
ldconfig通常在系统启动时运行,而当用户安装了一个新的动态链接库时,就需要手工运行这个命令。
6). 在使用时注意链接库
例如
g++ main.cpp -o main -lboost_thread
4. 测试
1). test_date.cpp

#include <boost/date_time/gregorian/gregorian.hpp> 
#include <iostream> 
int main() 
{ 
    boost::gregorian::date d(boost::gregorian::day_clock::local_day());
    std::cout << d.year() << "-" << d.month() << "-" << d.day() << std::endl; 
}

g++ test_date.cpp -o test_date
(or g++ -I /usr/local/boost/include -L /usr/local/boost/lib test_date.cpp -o test_date)
./test_date
2). test.cpp

#include <boost/lambda/lambda.hpp>  
#include <iostream>  
#include <iterator>  
#include <algorithm>  

int main()  
{  
    using namespace boost::lambda;  
    typedef std::istream_iterator<int> in;  

    std::for_each(  
        in(std::cin), in(), std::cout << (_1 * 10) << " " );  
} 

g++ test.cpp -o test
./test
3). test2.cpp

#include <iostream>  
#include <boost/filesystem.hpp>  

using namespace boost::filesystem;  

int main(int argc, char *argv[])  
{  
if (argc < 2) {  
    std::cout << "Usage: tut1 path\n";  
    return 1;  
}  
std::cout << argv[1] << " " << file_size(argv[1]) << std::endl;  
return 0;  
}  

g++ test2.cpp -o test2 -lboost_system -lboost_filesystem
./test2 test2
4).

#include <iostream>
#include <string>
using namespace std;

#include <boost/lexical_cast.hpp>
using namespace boost;

int main(int argc, char* argv[])
{
    int i = 888;
    string str = "999";
    i = boost::lexical_cast<int>(str);//int,long,float,double
    cout << i << endl;//输出整数:999

    str = "123.123";
    float f = boost::lexical_cast<float>(str);
    cout << f << endl;//输出浮点数: 123.123 
    //string str = boost::lexical_cast<string>(123);//12.21
    //bool b = boost::lexical_cast<bool>("1");

    getchar();
    return 0;
}

g++ test2.cpp -o test3
./test3
others:

查看gcc/g++默认include路径
gcc -print-prog-name=cc1plus -v
g++ -print-prog-name=cc1plus -v

export CPLUS_INCLUDE_PATH=/usr/local/boost/include
export LIBRARY_PATH=/usr/local/boost/lib

列子借鉴,天下文章一大抄,汗。。。
这里写图片描述

猜你喜欢

转载自blog.csdn.net/a379039233/article/details/80493167