Boost :: Python在OSX中找不到C ++类

时间:2014-07-07 14:10:13

标签: python c++ macos boost boost-python

我正在将应用程序从Linux移植到OS X,并且Boost :: Python集成在运行时失败。

我正在暴露我的C ++类:

using namespace scarlet;

BOOST_PYTHON_MODULE(libscarlet) {
    using namespace boost::python;

    class_<VideoEngine, boost::noncopyable>("VideoEngine", no_init)
        .def("play", &VideoEngine::play)
        .def("pause", &VideoEngine::pause)
        .def("isPaused", &VideoEngine::isPaused)
        [...]
    ;
}

我正在导入这样的库:

try {
    boost::python::import("libscarlet");
} catch (boost::python::error_already_set & e) {
    PyErr_Print();
}

然后我将一个实例注入到全局Python命名空间中,如下所示:

void VideoEngine::init() {
    [...]
    try {
        auto main_module = boost::python::import("__main__");
        auto main_namespace = main_module.attr("__dict__");
        main_namespace["engine"] = boost::python::object(boost::python::ptr(this));
    } catch (boost::python::error_already_set & e) {
        PyErr_Print();
    }
    [...]
}

它在Linux中运行良好,但在OS X中抛出异常,PyErr_Print()返回TypeError: No Python class registered for C++ class scarlet::VideoEngine

据我所知,当通过Python解释器导入时,模块可以正常工作。它很难测试,因为它被设计为作为预构造实例注入,但类和函数如下所示:

$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import libscarlet
>>> libscarlet.VideoEngine
<class 'libscarlet.VideoEngine'>
>>> libscarlet.VideoEngine.play
<unbound method VideoEngine.play>

关于不兼容性在哪里的任何想法?

编辑: 我开始认为它可能与多线程有关,因为我的OS X实现使用了不同的线程结构,尽管此处详述的所有调用都发生在同一个线程中。这可能是造成这种问题的原因吗?可能不是问题,因为它在单线程模式下无法在MS Windows中运行。

1 个答案:

答案 0 :(得分:0)

我现在已经解决了这个问题。

它完全是由Boost :: Python静态编译引起的,一旦我将其重新编译为共享库,问题就完全消失在所有平台上。

教训:不要静态编译提升。我很确定会有警告,应该注意它们。