2

私はCentOS、SWIG 1.3を使用しており、SWIGの例からサンプルのJavaの例をコンパイルするためにテストしました。それはから構成されています:

example.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;
}

example.i

%module example

extern int gcd(int x, int y);
extern double Foo;

次に、次のコマンドを使用します。

swig -java example.i

次に、生成されたexample_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)

example.iファイルが間違っているのですか、それとも何かを達成していませんか?または、これはSWIGのバグですか?回避策はありますか?

4

1 に答える 1

4

関数とグローバルが宣言されることをSWIGに伝えましたが、生成されたラッパーコードに宣言が表示されていることを確認する必要があります。( gccに高い警告設定を使用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"

適切な測定のために、example.cに対応するインクルードがあります。

これを書くための別のスタイルは次のようになります。

%module example

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

ただし、通常は%inline、ラッピングするものがラッピングのプロセスに固有であり、ラッピングするライブラリの一般的な部分ではない場合にのみ使用することをお勧めします。

于 2012-07-30T13:31:59.637 に答える