捣鼓了1天多才完成,sign。。。。
正确步骤如下:
1 安装boost.python
单独编译boost.python:
bjam -sTOOLS=gcc --with-python --build-type=complete
编译所有:
bjam -sTOOLS=gcc --build-type=complete
清除所有编译:
bjam -sTOOLS=gcc --clean
清除boost.python的编译文件:
bjam -sTOOLS=gcc --with-python --with-python
默认情况下,会在boost源文件下创建了bin.v2文件夹,该文件夹为编译后的二进制库
lwj@lwj-desktop:~/boost_1_37_0/bin.v2
2 创建工程
cd 到 boost_1_37_0/libs/python/example/quickstart
里面是一个样板项目,最为重要的2个文件是Jamroot 和 boost-build.jam
每个项目都要有这两个文件,里面需要配置boost目录。
我的boost-build.jam的内容:
Jamroot的内容:
3 使用bjam编译工程
创建 hello.cpp 文件
创建hello.py
模块hello_ext为c++所wrap.
在工程目录下,输入bjam
或者 bjam --preserve-test-targets 这样不会删除可执行文件(我这里是so文件)
编译若成功,会产生bin目录,该目录有gcc-4.2.4 hello.test 文件夹,
hello.test 为测试代码,里面有测试结果,测试结果为文本文件。
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
EXIT STATUS: 0
gcc-4.2.4文件夹中:
包含了hello_ext.so 文件,该文件可以做为python模块导入
4 运行加载库
进入hello_ext.so的目录,调出python命令行,结果会出错,信息如下:
lwj@lwj-desktop:~/code/python/embedc/helloworld/bin/gcc-4.2.4/debug$ ls
hello_ext.so hello.o
lwj@lwj-desktop:~/code/python/embedc/helloworld/bin/gcc-4.2.4/debug$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello_ext
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libboost_python-gcc42-d-1_37.so.1.37.0: cannot open shared object file: No such file or directory
>>>
原因是系统不认得boost.python的安装路径,因此需要设置 LD_LIBRARY_PATH
可以在控制台中输入 export LD_LIBRARY_PATH=path_to_your_libboost_python-gcc41-1_37.so.1.37.0
则此次会话可以成功import hello_ext
并且调用hello_ext.greet()方法
为使得每次终端都认得这个boost.python库的路径
最终。。。。混编之旅变开始拉,哈哈哈哈哈哈哈哈
正确步骤如下:
1 安装boost.python
单独编译boost.python:
bjam -sTOOLS=gcc --with-python --build-type=complete
编译所有:
bjam -sTOOLS=gcc --build-type=complete
清除所有编译:
bjam -sTOOLS=gcc --clean
清除boost.python的编译文件:
bjam -sTOOLS=gcc --with-python --with-python
默认情况下,会在boost源文件下创建了bin.v2文件夹,该文件夹为编译后的二进制库
lwj@lwj-desktop:~/boost_1_37_0/bin.v2
2 创建工程
cd 到 boost_1_37_0/libs/python/example/quickstart
里面是一个样板项目,最为重要的2个文件是Jamroot 和 boost-build.jam
每个项目都要有这两个文件,里面需要配置boost目录。
我的boost-build.jam的内容:
- boost-build /home/lwj/boost_1_37_0/tools/build/v2 ;
- # Copyright David Abrahams 2006. Distributed under the Boost
- # Software License, Version 1.0. (See accompanying
- # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- # Specify the path to the Boost project. If you move this project,
- # adjust the path to refer to the Boost root directory.
- use-project boost
- : /home/lwj/boost_1_37_0 ;
- # Set up the project-wide requirements that everything uses the
- # boost_python library defined in the project whose global ID is
- # /boost/python.
- project hello_ext
- : requirements <library>/boost/python//boost_python
- ;
- # Make the definition of the python-extension rule available
- import python ;
- # Declare a Python extension called hello.
- python-extension hello_ext : hello.cpp ;
- # Declare an executable called embedding that embeds Python
- #exe embedding : embedding.cpp /python//python ;
- #import testing ;
- # Declare a test of the extension module
- #testing.make-test run-pyd : extending test_extending.py : : test_ext ;
- # Declare a test of the embedding application
- #testing.run embedding
- # : # any ordinary arguments
- # : script.py # any arguments that should be treated as relative paths
- # : # requirements
- # : test_embed ; # name of test
- # Create a "test" target that runs all the tests
- #alias test : test_ext test_embed ;
- # make sure the tests don't run by default
- #explicit test_ext test_embed test ;
- # A little "rule" (function) to clean up the syntax of declaring tests
- # of these extension modules.
- local rule run-test ( test-name : sources + )
- {
- import testing ;
- testing.make-test run-pyd : $(sources) : : $(test-name) ;
- }
- # Declare test targets
- run-test hello : hello_ext hello.py ;
3 使用bjam编译工程
创建 hello.cpp 文件
- /*
- * =====================================================================================
- *
- * Filename: hello.cpp
- *
- * Description: test boost.python
- *
- * Version: 1.0
- * Created: 2008年12月17日 14时23分41秒
- * Revision: none
- * Compiler: gcc
- *
- * Author: Li WeiJian (mn), lwj1396@163.com
- * Company: hunan university
- *
- * =====================================================================================
- */
- #include<boost/python.hpp>
- char const* greet()
- {
- return "hello world";
- }
- BOOST_PYTHON_MODULE(hello_ext)
- {
- using namespace boost::python;
- def("greet",greet);
- }
- import hello_ext
- for i in range(10):
- print hello_ext.greet()
在工程目录下,输入bjam
或者 bjam --preserve-test-targets 这样不会删除可执行文件(我这里是so文件)
编译若成功,会产生bin目录,该目录有gcc-4.2.4 hello.test 文件夹,
hello.test 为测试代码,里面有测试结果,测试结果为文本文件。
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
EXIT STATUS: 0
gcc-4.2.4文件夹中:
包含了hello_ext.so 文件,该文件可以做为python模块导入
4 运行加载库
进入hello_ext.so的目录,调出python命令行,结果会出错,信息如下:
lwj@lwj-desktop:~/code/python/embedc/helloworld/bin/gcc-4.2.4/debug$ ls
hello_ext.so hello.o
lwj@lwj-desktop:~/code/python/embedc/helloworld/bin/gcc-4.2.4/debug$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello_ext
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libboost_python-gcc42-d-1_37.so.1.37.0: cannot open shared object file: No such file or directory
>>>
原因是系统不认得boost.python的安装路径,因此需要设置 LD_LIBRARY_PATH
可以在控制台中输入 export LD_LIBRARY_PATH=path_to_your_libboost_python-gcc41-1_37.so.1.37.0
则此次会话可以成功import hello_ext
并且调用hello_ext.greet()方法
为使得每次终端都认得这个boost.python库的路径
linux也可以支持“加载当前目录的动态库”。只要设置合适的环境变量LD_LIBRARY_PATH就可以了。设置方法有以下三种:
1、临时修改,log out之后就失效
在terminal中执行:export LD_LIBRARY_PATH=./
在terminal中执行:export LD_LIBRARY_PATH=./
2、让当前帐号以后都优先加载当前目录的动态库
修改~/.bash_profile在文件末尾加上两行: LD_LIBRARY_PATH=./ 和 export LD_LIBRARY_PATH
修改~/.bash_profile在文件末尾加上两行: LD_LIBRARY_PATH=./ 和 export LD_LIBRARY_PATH
3、让所有帐号从此都优先加载当前目录的动态库
修改/etc/profile在文件末尾加上两行: LD_LIBRARY_PATH=./ 和 export LD_LIBRARY_PATH
修改/etc/profile在文件末尾加上两行: LD_LIBRARY_PATH=./ 和 export LD_LIBRARY_PATH
PS:修改ld.so.conf不能达到我们的目的,因为ld.so.conf只支持绝对路径。
最终。。。。混编之旅变开始拉,哈哈哈哈哈哈哈哈