使用自定义的Python在Mac OS X Lion上提升Boost Python

5

我正在尝试将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个回答

3

我下载了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端口来帮助您。我没有尝试过,但看起来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

(提示:这是一个命令行代码示例,用于展示如何使用tab键自动补全一个变量名。)

1

看起来你正在将boost python与错误版本的python(2.7.0)链接,而将你的应用程序与正确版本的python(2.7.2)链接- PyRun_SimpleString与boost python无关,但是它是从你的测试应用程序直接调用Python API。

我使用一个project-config.jam文件(在boost构建目录中)来配置应该使用哪个python版本来构建boost-python,包括这一行(用于链接到我的简单2.7安装):

using python : 2.7 : /Library/Frameworks/Python.framework/Versions/2.7/ ;

感谢您的回答。我的project-config.jam文件包含using python : 2.7 : ../Python-2.7.2/ ;,但otool仍然列出了/System/Library/Framework条目。 - sharvey
@sharvey 我猜你已经尝试过进行干净构建,指定绝对路径,并且使用 using python : 2.7.2 : ... 等等了吧? - James
我无法使用 2.7.2,它会给我一个警告:警告:"using python" 需要一个由两部分(主版本号、次版本号)组成的版本号;而你输入的是 2.7.2。使用绝对路径也没有改变任何东西。在 Xcode 和 ./b2 --clean 中都进行了清理,但问题仍未解决... - sharvey
嗯,很明显boost python链接的是错误的版本。你没有覆盖你的版本的user-config.jam文件吗?也许值得将/System/Library/Frameworks/Python.framework/Versions/2.7中的python移开,因为这是boost python在构建和安装时所选中的。 - James

0

首先检查

可能是链接的动态库与实际使用的包含文件不匹配。一个简单的检查方法是在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"

您应该看到解释器、包含路径和二进制文件匹配(分别是解释器命令包含路径库路径)。

如果不匹配,请检查/usr/bin中的python-config文件:它应该指向python.org安装路径。如果没有,安装程序可能会错过它,需要修复。

第二个检查

可能链接的库在/usr/lib中没有正确设置。通过简单的ls -al /usr/lib/*python*,您应该看到一个指向您的python.org安装的/usr/lib/libpython2.7.dylib。如果没有,python.org安装程序可能会错过它,需要修复。

抱歉有点含糊不清。我一年前遇到了这个问题,并通过手动进行多次尝试来解决它,直到它起作用为止...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接