我最近使用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:属性名称必须是字符串,而不是'str'” 。哪个可疑听起来像一个占位符。
我在谷歌上查了一下,但没有运气。
我在Leopard上使用Boost v1.39,Python 2.5和GCC 4.0。
答案 0 :(得分:1)
您的代码适用于我,具有以下配置:
使用编译:
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