Java SWIG不';t在包装文件中声明全局变量

Java SWIG不';t在包装文件中声明全局变量,java,swig,Java,Swig,我使用的是Cent OS、SWIG 1.3,我已经测试过如何从SWIG示例编译示例Java示例。它包括: 例c /* A global variable */ double Foo = 3.0; /* Compute the greatest common divisor of positive integers */ int gcd(int x, int y) { int g; g = y; while (x > 0) { g = x; x = y % x;

我使用的是Cent OS、SWIG 1.3,我已经测试过如何从SWIG示例编译示例Java示例。它包括:

例c

/* A global variable */
double Foo = 3.0;

/* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {
  int g;
  g = y;
  while (x > 0) {
    g = x;
    x = y % x;
    y = g;
  }
  return g;
}
例如,我

%module example

extern int gcd(int x, int y);
extern double Foo;
然后我使用命令:

swig -java example.i
然后,我用以下代码编译生成的示例_wrap.c:

gcc -c example_wrap.c -I/usr/java/jdk1.6.0_24/include -I/usr/java/jdk1.6.0_24/include/linux
我有以下错误:

example_wrap.c: In function ‘Java_exampleJNI_Foo_1set’:
example_wrap.c:201: error: ‘Foo’ undeclared (first use in this function)

这是一个例子。我的文件错了还是我没有完成什么?或者这是SWIG中的一个bug?有解决方法吗?

您已经告诉SWIG将声明函数和全局变量,但是您需要确保声明在生成的包装器代码中可见。(如果没有,您可能还会收到一条关于
gcd
的隐式声明的警告)

解决方案是使该声明可见,最简单的方法是:

%module example

%{
// code here is passed straight to example_wrap.c unmodified
extern int gcd(int x, int y);
extern double Foo;
%}

// code here is wrapped:
extern int gcd(int x, int y);
extern double Foo;
就我个人而言,我会添加一个example.h文件,其中包含模块文件中的声明:

%module example

%{
// code here is passed straight to example_wrap.c unmodified
#include "example.h"
%}

// code here is wrapped:
%include "example.h"
在示例c中使用相应的include进行良好度量

另一种写作风格是:

%module example

%inline %{
  // Wrap and pass through to example_wrap.c simultaneously
  extern int gcd(int x, int y);
  extern double Foo;  
%}

但通常情况下,我只建议在以下情况下使用
%inline
,即您正在包装的内容是特定于包装过程的,而不是要包装的库的一般部分。

您已经告诉SWIG将声明函数和全局变量,但您需要确保声明在生成的包装器代码中可见。(如果没有,您可能还会收到一条关于
gcd
的隐式声明的警告)

解决方案是使该声明可见,最简单的方法是:

%module example

%{
// code here is passed straight to example_wrap.c unmodified
extern int gcd(int x, int y);
extern double Foo;
%}

// code here is wrapped:
extern int gcd(int x, int y);
extern double Foo;
就我个人而言,我会添加一个example.h文件,其中包含模块文件中的声明:

%module example

%{
// code here is passed straight to example_wrap.c unmodified
#include "example.h"
%}

// code here is wrapped:
%include "example.h"
在示例c中使用相应的include进行良好度量

另一种写作风格是:

%module example

%inline %{
  // Wrap and pass through to example_wrap.c simultaneously
  extern int gcd(int x, int y);
  extern double Foo;  
%}

但通常情况下,我只建议使用
%inline
来包装特定于包装过程的内容,而不是要包装的库的一般部分。

非常感谢您的帮助。:)非常感谢您的帮助。:)