用Shiboken生成C ++库python绑定

时间:2014-10-18 17:51:57

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

我在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动态链接,所以我想foo​​lib模块工作所需的所有符号都在那里;)

这里是代码:

#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:没有名为foolib的模块

然后第二次运行.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,但似乎不仅仅是链接绑定动态库。

1 个答案:

答案 0 :(得分:0)

Shiboken命令行的包含路径不包含foo.h的路径。我无法判断这是否是您问题的原因,但如果我这样做,则无法生成以下文件:

  • math_wrapper.cpp
  • math_wrapper.h

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