下载:
wget http://101.96.10.75/ncu.dl.sourceforge.net/project/boost/boost/1.62.0/boost_1_62_0.tar.bz2
tar xf boost_1_62_0.tar.bz2
cd boost_1_62_0/
编译安装
bash bootstrap.sh
echo $?
./b2
echo $?
sudo ./b2 install --prefix=/tmp/haha #/tmp/haha是我建立的目录
echo $?
chunli@linux:~$ ll /tmp/haha/
total 8.0K
drwxr-xr-x 3 root root 4.0K Oct 18 20:58 include
drwxr-xr-x 2 root root 4.0K Oct 18 20:59 lib
chunli@linux:~$ du -sh /tmp/haha/
184M/tmp/haha/
chunli@linux:~$
写个测试程序:
root@linux:/tmp# cat main.cpp
#include
#include
using namespace std;
int main()
{
using boost::lexical_cast;
int a=lexical_cast("123");
double b=lexical_cast("123.0123456789");
string s0=lexical_cast(a);
string s1=lexical_cast(b);
cout<
cout<
int c=0;
try{
c=lexical_cast("abcd");
}
catch(boost::bad_lexical_cast& e){
cout<
}
cout <
return 0;
}
root@linux:/tmp# g++ main.cpp && ./a.out
number: 123 123.012
string: 123 123.0123456789
bad lexical cast: source type value could not be interpreted as target
Hello
root@linux:/tmp#
编译报错:
chunli@ubuntu14:/tmp$ g++ main.cpp
main.cpp:1:39: fatal error: boost/lexical_cast.hpp: No such file or directory
#include
^
compilation terminated.
解决办法:指定头文件
chunli@ubuntu14:/tmp$ g++ main.cpp -I haha/include/
chunli@ubuntu14:/tmp$ ./a.out
number: 123 123.012
string: 123 123.0123456789
bad lexical cast: source type value could not be interpreted as target
Hello
chunli@ubuntu14:/tmp$
永久的解决办法,放到系统的路径下
chunli@ubuntu14:/tmp$ sudo mv haha/include/* /usr/include/
chunli@ubuntu14:/tmp$ sudo mv haha/lib/ /usr/lib/
chunli@ubuntu14:/tmp$ g++ main.cpp && ./a.out
number: 123 123.012
string: 123 123.0123456789
bad lexical cast: source type value could not be interpreted as target
Hello
chunli@ubuntu14:/tmp$