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

列子借鉴,天下文章一大抄,汗。。。
è¿éåå¾çæè¿°


————————————————
版权声明:本文为CSDN博主「宇宙379」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/a379039233/article/details/80493167

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值