在 Java 中处理 FILE * C 输入参数的 SWIG 配置
如何配置 SWIG .i 文件来处理 C FILE * 类型?下面的函数设置一个文件,以便可以将日志输出写入其中。我需要从 Java 类中调用 if 。目前,当我仅将 C 头文件包含在以下函数中时,SWIG 会生成 public static void setLogFile(SWIGTYPE_p_FILE fd) 函数。有什么想法吗?
C 函数:
void setLogFile(FILE *fd);
我尝试使用下面的#1 并得到以下异常:
test.i:
%module Example
%{
#include "headerLogFile.h"
%}
%inline %{
void setLogFile(const char *fn) {
FILE *f = fopen(fn, "w");
setLogFile(f);
}
%}
%ignore setLogFile;
%include "headerLogFile.h"
Exception:
[exec] test_wrap.c:193: error: conflicting types for 'setLogFile'
[exec] /test/include/headerLogFile.h:96: error: previous declaration of 'setLogFile' was here
[exec] test_wrap.c: In function `setLogFile':
[exec] test_wrap.c:195: warning: passing arg 1 of `setLogFile' from incompatible pointer type
How would you configure the SWIG .i file to handle the C FILE * type? The below function sets a file so that log output can be written to it. I need to call if from a Java class. Currently a public static void setLogFile(SWIGTYPE_p_FILE fd) function is generated by SWIG when I just include the C header file with the below function. Any ideas?
C Function:
void setLogFile(FILE *fd);
I attempted #1 using the below and got the below exception:
test.i:
%module Example
%{
#include "headerLogFile.h"
%}
%inline %{
void setLogFile(const char *fn) {
FILE *f = fopen(fn, "w");
setLogFile(f);
}
%}
%ignore setLogFile;
%include "headerLogFile.h"
Exception:
[exec] test_wrap.c:193: error: conflicting types for 'setLogFile'
[exec] /test/include/headerLogFile.h:96: error: previous declaration of 'setLogFile' was here
[exec] test_wrap.c: In function `setLogFile':
[exec] test_wrap.c:195: warning: passing arg 1 of `setLogFile' from incompatible pointer type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
给定 test.h 如下所示:
我可以看到您可能选择采用三种方法来包装此函数:
方法 1 - 从 Java 传递
String
:向 Java 公开一个需要传递文件名的函数作为
String
,而不是FILE*
:这使用
%inline
指示 SWIG 在定义该函数的同时包装该函数。如果您仍然使用%include "test.h"
那么您可能需要 从 SWIG 隐藏原始版本。方法 2 - 包装更多 stdio.h:
包装不仅仅是
setLogFile
,适当包装fopen
、fmemopen
等内容。 (我个人不太喜欢这个解决方案,所以我没有为其提供示例)方法 3 - 公开一个采用
FileOutputStream
:这会执行以下操作:
java.io.FileOutputStream
。jobject
int
字段在FileDescriptor
类中(参见此处)。这可能是不可移植的,并且读取类的私有部分通常被认为是不好的,但它允许我们获取可以传递给 fdopen() 的东西,以获取FILE*
“真正的”调用FILE*
无效。我们通过在private static
变量中保留对FileOutputStream
的引用来实现此目的。前一个类型映射的pre="...
会导致保留最新的类型映射,直到我们更改为另一个类型映射。(如果我们确实调用setLogFile
两次就可以了,事实上,我们发布了对之前的FileOutputStream
的引用,这很好)Given test.h which looks like:
I can see three approaches you might chose to take to wrapping this function:
Method 1 - Pass a
String
from Java:Expose a function to Java that expects a file name passed as a
String
, not aFILE*
:This uses
%inline
to instruct SWIG to wrap this function at the same time as defining it. If you still use%include "test.h"
then you'd probably want to hide the original version from SWIG.Method 2 - Wrap more of stdio.h:
Wrap more than just
setLogFile
, wrap things likefopen
,fmemopen
etc. as appropriate. (I don't like this solution much personally so I've not made an example for it)Method 3 - Expose a Java interface that takes a
FileOutputStream
:This does the following things:
java.io.FileOutputStream
.java.io.FileDescriptor
instead however.jobject
int
field in theFileDescriptor
class (see here). This is probably not portable and reading private parts of classes is generally considered bad, but it allows us to get something we can pass tofdopen()
to get aFILE*
for the "real" callFileOutputStream
and callsgetFD()
on it to get theFileDescriptor
object for it. It also adds an exception specification to matchgetFD()
and performs one other function which is part of the next pointFileOutputStream
, which would close the file handle and invalidate ourFILE*
. We do this by keeping a reference to theFileOutputStream
we were given in aprivate static
variable. Thepre="...
of the previous typemap causes the most recent one to be retained until we change to another one. (If we do callsetLogFile
twice it's OK and in fact good that we release our reference to the previousFileOutputStream
)