在Mac OS X Lion上使用自定义Python编译Boost Python
我正在尝试让boost python与一个自定义的python库一起工作。我有一个python源代码,并使用以下命令构建boost.python:
./bootstrap.sh --with-python-root=../Python-2.7.2 --with-libraries=python
然后运行 ./b2
但是当我在我的应用程序中尝试使用boost.python时,出现了这个错误:
Fatal Python error: Interpreter not initialized (version mismatch?)
当我调用 PyRun_SimpleString("import sys\nprint sys.version");
时,我得到了2.7.2,这是我预期的结果(也是我用来构建boost.python的python版本,而不是系统版本)。
我是不是漏掉了什么?
当我检查这个动态库(dylib)链接了哪些库时,我得到了这个结果:
libboost_python.dylib (compatibility version 0.0.0, current version 0.0.0)
/System/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.1)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)`
在我的Xcode项目中,我包含了来自 --with-python-root 参数指定的文件夹中的python和boost/stage/lib的内容,
3 个回答
第一次检查
可能是链接的动态库(dylib)和实际使用的包含文件不匹配。一个简单的检查方法是给bjam加上选项 --debug-configuration
,然后查看包含和链接的路径:
./b2 -q --prefix=your_install_dir YOU_OTHER_OPTIONS --debug-configuration install
这应该在开头显示这些行:
notice: [python-cfg] Configuring python...
notice: [python-cfg] user-specified version: "2.7"
notice: [python-cfg] user-specified cmd-or-prefix: "/Library/Frameworks/Python.framework/Versions/2.7"
notice: [python-cfg] Checking interpreter command "/Library/Frameworks/Python.framework/Versions/2.7/bin/python"...
notice: [python-cfg] running command '/Library/Frameworks/Python.framework/Versions/2.7/bin/python -c "from sys import *; print('version=%d.%d\nplatform=%s\nprefix=%s\nexec_prefix=%s\nexecutable=%s' % (version_info[0],version_info[1],platform,prefix,exec_prefix,executable))" 2>&1'
notice: [python-cfg] ...requested configuration matched!
notice: [python-cfg] Details of this Python configuration:
notice: [python-cfg] interpreter command: "/Library/Frameworks/Python.framework/Versions/2.7/bin/python"
notice: [python-cfg] include path: "/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7"
notice: [python-cfg] library path: "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config" "/Library/Frameworks/Python.framework/Versions/2.7/lib"
notice: [python-cfg] framework directory is "/Library/Frameworks/Python.framework"
你应该能看到解释器、包含路径和库路径是匹配的(分别是 interpreter command
、include path
和 library path
)。
如果不匹配,检查一下 python-config
文件,位置在 /usr/bin
:它应该指向python.org的安装路径。如果不是,那说明安装程序漏掉了这个,需要修复一下。
第二次检查
可能是链接的库在 /usr/lib
中没有正确设置。通过简单地运行 ls -al /usr/lib/*python*
,你应该能看到一个指向你python.org安装的 /usr/lib/libpython2.7.dylib
。如果没有,那说明python.org的安装程序漏掉了这个,也需要修复。
抱歉说得有点模糊。我一年前遇到过这个问题,花了很多时间手动修复,直到它正常工作为止……
看起来你在链接boost python的时候用了错误的Python版本(2.7.0),而你的应用程序却是用正确的版本(2.7.2)。其实,PyRun_SimpleString
和boost python没有关系,它只是你测试应用程序直接调用Python API的一部分。
我在boost的构建目录里使用一个叫project-config.jam
的文件来配置boost-python应该用哪个Python版本来构建,其中包括这一行(用于链接我简单的2.7版本安装):
using python : 2.7 : /Library/Frameworks/Python.framework/Versions/2.7/ ;
我下载了boost python,并且用我通过Mac Ports安装的自定义Python编译了一下,结果一切正常。
我的步骤是……
$ /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python
Python 2.7.2 (default, Nov 17 2011, 00:52:26)
$ sudo ./bootstrap.sh --with-libraries=python --with-python-root=/opt/local/Library/Frameworks/Python.framework/Versions/2.7
$ ./b2
$ cd /Users/YourName/Downloads/boost_1_48_0/libs/python/example/tutorial
$ ../../../../bjam
...patience...
...patience...
...found 1577 targets...
...updating 12 targets...
common.mkdir bin
common.mkdir bin/darwin-4.2.1
common.mkdir bin/darwin-4.2.1/debug
darwin.compile.c++ bin/darwin-4.2.1/debug/hello.o
darwin.link.dll bin/darwin-4.2.1/debug/hello_ext.so
common.copy libboost_python.dylib
common.copy hello_ext.so
common.mkdir bin/hello.test
common.mkdir bin/hello.test/darwin-4.2.1
common.mkdir bin/hello.test/darwin-4.2.1/debug
capture-output bin/hello.test/darwin-4.2.1/debug/hello
**passed** bin/hello.test/darwin-4.2.1/debug/hello.test
...updated 12 targets...
$ ls
Jamroot hello.cpp hello_ext.so
bin hello.py libboost_python.dylib
$ python
Python 2.7.2 (default, Nov 17 2011, 00:52:26)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello_ext
>>> hello_ext.greet()
'hello, world'
>>>
另外,如果你不想自己动手编译所有东西,可以利用Mac Ports来帮忙。我没试过,但看起来boost.python是可以用的,不过是1.47版本,而不是1.48。
$ port info boost
boost @1.47.0, Revision 2 (devel)
Variants: debug, no_single, no_static, openmpi, python24, python25, python26, python27, python31, python32, regex_match_extra,
universal
Description: Boost provides free portable peer-reviewed C++ libraries. The emphasis is on portable libraries which work well with
the C++ Standard Library.
Homepage: http://www.boost.org
Library Dependencies: zlib, expat, bzip2, icu
Platforms: darwin
License: Boost-1.0
Maintainers: adfernandes@macports.org
其实,为了解决这个问题,我们可以看看我们的环境,比较一下你是否还有问题 :)。
$ echo $press-TAB