【问题标题】:C++ library python binding generation with Shiboken使用 Shiboken 生成 C++ 库 python 绑定
【发布时间】:2014-10-18 17:51:57
【问题描述】:

我按照 wiki http://qt-project.org/wiki/PySide_Binding_Generation_Tutorial 上的教程进行操作 但我无法让它正常工作。我在 MacOSX 上

到目前为止,这是我所做的:

  • 构建 FooLib(静态)---> libFooLib.a
  • 创建 typesystem_foo.xml
  • 使用以下命令运行 shiboken:

    shiboken-2.7 global.h --include-paths=.:/opt/local/include/PySide-2.7:/opt/local/include --typesystem-paths=/opt/local/share/PySide-2.7 /typesystems --output-directory=../FooLibBinding typesystem_foo.xml

  • 从生成的 c++ 代码构建 FooLibBinding 动态库 --> libFooLibBinding.dylib

现在,我不再只是从命令行运行 python 解释器,而是创建了一个 C++ 程序,该程序将加载 python 解释器并使用 FooLib 打开一个 .py 脚本。这个程序与 libFooLibBinding.dylib 动态链接,所以我猜所有的符号都需要让傻瓜模块工作;)

代码如下:

#include <iostream>
#include <Python.h>

int main(int argc, char* argv[])
{

    ///Python init
    Py_SetProgramName(argv[0]);
    Py_Initialize();
    PySys_SetArgv(argc, argv); /// relative module import

    ///Try out loading the module, this is just for testing
    /// -----------
    PyObject *sysPath = PySys_GetObject("path");
    PyObject *path = PyString_FromString("/Users/alexandre/Downloads/BindingTest");
    int result = PyList_Insert(sysPath, 0, path);
    PyObject *pModule = PyImport_ImportModule("foolib");
    if (PyErr_Occurred())
            PyErr_Print();
    /// -----------

    ///Our python file to interpret
    const char* filename = "/Users/alexandre/Downloads/BindingTest/FooLibTest/foolib_test.py";
    FILE* file = fopen(filename,"r");
    PyRun_SimpleFile(file,filename);

    ///close python
    Py_Finalize();
    return 0;
}

运行程序时,第一次尝试加载模块时失败: ImportError: 没有名为傻瓜的模块

然后第二次运行 .py 脚本:

Traceback (most recent call last):
  File "/Users/alexandre/Downloads/BindingTest/FooLibTest/foolib_test.py", line 1, in <module>
    from foolib import FooClass
ImportError: No module named foolib

显然它找不到从绑定生成的模块。我的问题是我应该怎么做才能找到它?

本教程使用了一个 Makefile,但除了链接绑定动态库之外似乎没有做更多的事情。

【问题讨论】:

    标签: python c++ qt pyside python-bindings


    【解决方案1】:

    Shiboken 命令行的包含路径不包含 foo.h 的路径。我不知道这是否是您的问题的原因,但如果我这样做了,它不会生成以下文件:

    • math_wrapper.cpp
    • math_wrapper.h

    ...您显然需要能够编译对 foo 库中 Maths 类的支持。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多