【问题标题】:SWIG (C & Python) cstring.i problemsSWIG (C & Python) cstring.i 问题
【发布时间】:2015-10-16 15:52:14
【问题描述】:

我已阅读 http://www.swig.org/Doc1.3/Library.html#Library_nn12 并且在 Python 中使用以下形式的函数时遇到问题:

/* foo.h */
//fills *filename with useful stuff in the usual way
int foo(char *filename);

我有一个界面文件大致如下:

%module foo
{% #include "foo.h" %}
%include "foo.h"

%cstring_bounded_output(char *filename, 1024);
extern int foo(char *filename);

swig 文档让我相信我可以不带参数地从 python 调用 foo() 并且返回值将是 python 字符串文件名。然而:

In [4]: foo.foo()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-db4ddc3a33ec> in <module>()
----> 1 foo.foo()

TypeError: foo() takes exactly 1 argument (0 given)

foo() 仍然期待一个 char *。我的接口文件是否正确?这是从这种类型的 C 函数在 python 中获取值的首选方法吗?

http://web.mit.edu/svn/src/swig-1.3.25/Lib/python/cstring.i 很难解析。如果有人知道这个宏在做什么,最好能对它有所了解。

注意:如果相关的话,我被困在使用 Swig 1.3.z 的生产环境中。此外,我必须在这里使用 Swig。

【问题讨论】:

    标签: python c swig


    【解决方案1】:

    %cstring_bounded_output 必须foo 的声明之前,即使是在头文件中的声明。这个版本适合我:

    %module foo
    
    %include cstring.i
    
    %{
    #include "foo.h"
    %}
    
    %cstring_bounded_output(char* filename, 1024);
    %include "foo.h"
    
    extern int foo(char *filename);
    

    【讨论】:

    • 订购正是问题所在。感谢您的快速回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多