【问题标题】:Boost::Python Not Finding C++ Class in OSXBoost::Python 在 OSX 中找不到 C++ 类
【发布时间】:2014-07-07 14:10:12
【问题描述】:

我正在将应用程序从 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 中不起作用。

【问题讨论】:

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


    【解决方案1】:

    我现在已经解决了。

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

    教训:不要静态编译 boost。我很确定有针对它的警告,应该注意它们。

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 1970-01-01
      • 2011-03-08
      • 2016-08-21
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      相关资源
      最近更新 更多