下载:
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 <boost/lexical_cast.hpp> #include <iostream> using namespace std; int main() { using boost::lexical_cast; int a=lexical_cast<int>("123"); double b=lexical_cast<double>("123.0123456789"); string s0=lexical_cast<string>(a); string s1=lexical_cast<string>(b); cout<<"number: "<<a<<" "<<b<<endl; cout<<"string: "<<s0<<" "<<s1<<endl; int c=0; try{ c=lexical_cast<int>("abcd"); } catch(boost::bad_lexical_cast& e){ cout<<e.what()<<endl; } cout << "Hello \n"; 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 <boost/lexical_cast.hpp> ^ 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$
本文出自 “魂斗罗” 博客,谢绝转载!
原文地址:http://990487026.blog.51cto.com/10133282/1863180