7

我最近使用 MacPorts 安装了 Boost,目的是在 C++ 中嵌入一些 Python。然后我决定使用 Python 网站上的示例检查我是否正确配置了 Xcode:

#include <boost/python.hpp>

using namespace boost::python;

int main( int argc, char ** argv ) 
{
    try 
    {
        Py_Initialize();

        object main_module(handle<>(borrowed(PyImport_AddModule("__main__"))));

        object main_namespace = main_module.attr("__dict__");

        handle<> ignored(PyRun_String("print \"Hello, World\"",
                                      Py_file_input,
                                      main_namespace.ptr(),
                                      main_namespace.ptr()));
    } 
    catch( error_already_set ) 
    {
        PyErr_Print();
    }
}

它编译正确,但是当我启动它时,对 attr() 的调用会引发异常,并且生成的错误消息是"TypeError: attribute name must be string, not 'str'"。这听起来像是一个占位符。

我在谷歌上查了一下,但没有运气。

我在 Leopard 上使用 Boost v1.39、Python 2.5 和 GCC 4.0。

4

1 回答 1

1

您的代码使用以下配置为我工作:

  • 雪豹
  • gcc 版本 4.2.1 (AppleInc. build 5646)
  • Boost 1.41.0 安装到 /usr/local/boost/1_41_0/
  • 股票 OSX Python 2.5

编译使用:

g++ -I/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/ -I/usr/local/boost/1_41_0/include -L/usr /local/boost/1_41_0/lib/ -boost_python -L/usr/lib/python2.6/config -lpython2.6 test.cpp

于 2010-07-06T13:16:15.167 回答