Linux编译安装boost1.48

1.下载站:
http://sourceforge.net/projects/boost/files/boost/1.48.0/
2.安装
tar -xzvf boost_1_48_0.tar.g -C ~/source
cd source/boost_1_48_0/
./bootstrap.sh
sudo ./bjam  --layout=versioned --build-type=complete --toolset=gcc install

3.设置环境变量
        在/etc/profile.d新建可执行文件boost.sh,添加文件内容:
#!/bin/sh
BOOST_INCLUDE=/usr/local/include/boost 
BOOST_LIB=/usr/local/lib
export BOOST_INCLUDE BOOST_LIB 

命令行执行:source /etc/profile.d/boost.sh
        以后在编译程序时,只需要用:-I$BOOST_INCLUDE -L$BOOST_LIB 即可,还要使用-l指定了链接库。
4.设置共享库链接目录
/etc/ld.so.conf文件内容后面追加/usr/local/lib
命令行执行
sudo ldconfig

5.遇到的问题及解决方案
安装过程爆出很多和bz相关的错误,解决方案如下
 sudo apt-get install libbz2-dev

PS 安装过程中的错误:
libs/iostreams/src/bzip2.cpp:20:56: error: bzlib.h: 没有那个文件或目录
libs/iostreams/src/bzip2.cpp:31: error: ‘BZ_OK’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:32: error: ‘BZ_RUN_OK’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:33: error: ‘BZ_FLUSH_OK’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:34: error: ‘BZ_FINISH_OK’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:35: error: ‘BZ_STREAM_END’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:36: error: ‘BZ_SEQUENCE_ERROR’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:37: error: ‘BZ_PARAM_ERROR’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:38: error: ‘BZ_MEM_ERROR’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:39: error: ‘BZ_DATA_ERROR’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:40: error: ‘BZ_DATA_ERROR_MAGIC’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:41: error: ‘BZ_IO_ERROR’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:42: error: ‘BZ_UNEXPECTED_EOF’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:43: error: ‘BZ_OUTBUFF_FULL’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:44: error: ‘BZ_CONFIG_ERROR’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:48: error: ‘BZ_FINISH’ was not declared in this scope
//mostly like above....

6.测试
#include <boost/python.hpp> 
char const* greet()
{
   return "hello, world";
}

BOOST_PYTHON_MODULE(hello)
{
    using namespace boost::python;
    def("greet", greet);
}

把代码存为hello.cpp, 编译成so库
g++ hello.cpp -o hello.so -shared -I/usr/include/python2.6 -I /usr/lib/python2.6/config/ -lboost_python

此处python路径设为你的python路径, 并且必须加-lboost_python, 这个库名不一定是这个, 去/user/local/lib查
然后在有此so库的目录, 进入python, 可以如下使用
>>> import hello
>>> hello.greet()
'hello, world'

7.测试遇到问题及解决方案
如果遇到问题:
/usr/bin/ld: cannot find -lboost_python
collect2: ld returned 1 exit status

解决方案:
  • 确认/etc/ld.so.conf.d/相关配置文件已包含boost_python.so所在目录
  • boost_python库所在目录(默认安装在/usr/local/lib)存在libboost_python.so,若不存在则从libboost_python.so.***建软链接到libboost_python.so

添加新评论 »