【Todo】Boost安装与学习

现在这里找下载包

http://sourceforge.net/projects/boost

我找的是 1_62_0

下面是从公司wiki上找到的一个说明。

boost & thrift安装步骤
1.    boost安装
cd /usr/local
tar zxvf boost_1_49_0.tar.gz
./bootstrap.sh --prefix=/usr/local/boost_1_49_0
./b2 install


2.    thrift安装
tar zxvf thrift-0.8.0.tar.gz
cd thrift-0.8.0
./configure --with-boost=/usr/local/boost_1_49_0 --prefix=/home/work/local/thrift-0.8.0
make
make install


make如有下面报错:
…: tr1/functional: No such file or directory
…
make[4]: Leaving directory `/home/work/thrift-0.8.0/lib/cpp'
make[3]: *** [all-recursive] Error 1
make[3]: Leaving directory `/home/work/thrift-0.8.0/lib/cpp'
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory `/home/work/thrift-0.8.0/lib'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/work/thrift-0.8.0'
make: *** [all] Error 2

则修改如下3个文件:
vi lib/cpp/src/concurrency/ThreadManager.h
24 #include <boost/tr1/tr1/functional>

vi lib/cpp/src/async/TAsyncChannel.h
23 #include <boost/tr1/tr1/functional>

vi lib/cpp/src/async/TAsyncChannel.cpp
21 #include <boost/tr1/tr1/functional>

开始,我直接用

./bootstrap.sh
./b2
./b2 install

结果报了很多错,基本都是fail或者skip.

然后按照上面的提示,指定了一下目录:

./bootstrap.sh --prefix=/home/work/data/installed/boost
./b2 install

还是有一些skip的提示:

...failed updating 50 targets...
...skipped 35 targets...
...updated 13364 targets...

但是已经比之前好多了,已经成功更新了10000+targets.

在目录 /home/work/data/code/boost_demo 里面,写一段demo代码 boost_demo.cpp 如下:

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

int main() {
    using boost::lexical_cast;
    int a = lexical_cast<int> ("123");
    double b = lexical_cast<double> ("123.12");
    std::cout << a << std::endl;
    std::cout << b << std::endl;
    return 0;
}

编译命令如下:

g++ -Wall -I/home/work/data/installed/boost/include/ -o test_boost boost_demo.cpp

但是报错找不到头文件:

boost_demo.cpp:1:37: boost/lexical_cast.hpp : No such file or directory

分析了一下,是因为boost没有装在标准目录,所以需要用""代替<>,代码改成:

#include  "boost/lexical_cast.hpp"
#include  <iostream>

int main() {
    using boost::lexical_cast;
    int a = lexical_cast<int> ("123");
    double b = lexical_cast<double> ("123.12");
    std::cout << a << std::endl;
    std::cout << b << std::endl;
    return 0;
}

编译后运行:

g++ -Wall -I/home/work/data/installed/boost/include/ -o test_boost boost_demo.cpp

./test_boost

123
123.12

注意,boost大多数库一般都不需要添加-L或者-l参数,用-I指定头文件路径即可。要根据不同的库的情况来处理。

然后在我自己的Mac机器上,运行 brew search boost 查看,发现boost貌似已经安装,写程序实验了一下(仍然用上面的程序,""可以写成<>)。

编译,运行:

g++ -o boost_demo boost_demo.cpp

./boost_demo

123
123.12

以上。后续,再对boost做更多学习和实验。

原文地址:https://www.cnblogs.com/charlesblc/p/5945350.html