2015-10-16 101 views
0

我已閱讀http://www.swig.org/Doc1.3/Library.html#Library_nn12並使用以下形式的功能時遇到在Python麻煩:SWIG(C&Python)的cstring.i問題

/* 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 doc使我相信我可以從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。

回答

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); 
+0

排序正是問題所在。感謝您及時的回覆。 – djh