gpt4 book ai didi

python - SWIG(C 和 Python)cstring.i 问题

转载 作者:行者123 更新时间:2023-11-30 15:16:14 27 4
gpt4 key购买 nike

我已阅读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);

我有一个接口(interface)文件,大致如下所示:

%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 *。我的接口(interface)文件正确吗?这是从这种类型的 C 函数获取 python 值的首选方法吗?

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

注意:我陷入了使用 Swig 1.3.z 的生产环境(如果相关的话)。此外,我必须在这里使用 Swig。

最佳答案

%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);

关于python - SWIG(C 和 Python)cstring.i 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33174664/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com