2

Python にインポートする必要がある C++ クラスがあります。そのために、私は SWIG を使用しています。以下はswigインターフェースです

たとえば .i

/* File: example.i */
%module example

%{
#include "Item.h"
#include "GradedDouble.h"
#include "GradedComplex.h"
%}

%include <std_string.i>
%include <std_complex.i>
%include "Item.h"
%include "GradedDouble.h"
%include "GradedComplex.h"  

%template(Int) Item<int>;
%template(Complex) Item<std::complex<double> >;

そして、ラッパークラスとpythonモジュールを作成するために、Windows XP環境で次のコマンドを実行しています

c:\>swig -c++ -python -nodefaultctor example.i 
c:\>python setup.py build_ext --inplace

2 番目のコマンドを実行した後、次のエラーが発生します。

*C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\Python26\libs /LIBPATH:C:\Python26\PCbuild /EXPORT:init_example build\temp.win32-2.6\Release\example_wrap.obj "/OUT:C:\Documents and Settings\swig_example.pyd" /IMPLIB:build\temp.win32-2.6\Release\_example.lib /MANIFESTFILE:build\temp.win32-2.6\Release\_example.pyd.manifest
   Creating library build\temp.win32-2.6\Release\_example.lib and object build\temp.win32-2.6\Release\_example.exp
example_wrap.obj : error LNK2019: unresolved external symbol "public: __thiscall GradedDouble::~GradedDouble(void)" (??1GradedDouble@@QAE@XZ) referenced in function __wrap_delete_GradedDouble
example_wrap.obj : error LNK2019: unresolved external symbol "public: __thiscall GradedComplex::~GradedComplex(void)" (??1GradedComplex@@QAE@XZ) referenced in function __wrap_delete_GradedComplex
example_wrap.obj : error LNK2019: unresolved external symbol "public: __thiscall GradedDouble::GradedDouble(int,double *)" (??0GradedDouble@@QAE@HPAN@Z) referenced in function __wrap_new_GradedDouble
example_wrap.obj : error LNK2019: unresolved external symbol "public: void __thiscall GradedDouble::avg(double *)" (?avg@GradedDouble@@QAEXPAN@Z) referenced in function __wrap_GradedDouble_avg
example_wrap.obj : error LNK2019: unresolved external symbol "public: __thiscall GradedComplex::GradedComplex(int,double *)" (??0GradedComplex@@QAE@HPAN@Z) referenced in function __wrap_new_GradedComplex
example_wrap.obj : error LNK2019: unresolved external symbol "public: void __thiscall GradedComplex::avg(double *)" (?avg@GradedComplex@@QAEXPAN@Z) referenced in function __wrap_GradedComplex_avg
example_wrap.obj : error LNK2019: unresolved external symbol "public: void __thiscall GradedComplex::push(class Item<class std::complex<double> >)" (?push@GradedComplex@@QAEXV?$Item@V?$complex@N@std@@@@@Z) referenced in function __wrap_GradedComplex_push
example_wrap.obj : error LNK2019: unresolved external symbol "public: void __thiscall GradedDouble::push(class Item<double>)" (?push@GradedDouble@@QAEXV?$Item@N@@@Z) referenced in function __wrap_GradedDouble_push
C:\Documents and Settings\swig_example.pyd : fatal error LNK1120: 8 unresolved externals
error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe"'failed with exit status 1120*

swig (example.i) のインターフェイス ファイルの作成に問題があるようです。インターフェイス ファイルの作成についてヘルプが必要です。以下はヘッダーファイルです

GradedComplex.h

#ifndef __GRADEDCOMPLEX_H__
#define __GRADEDCOMPLEX_H__
#include <complex>
#include <set>
#include <vector>
#include "Item.h"
class GradedComplex
{
public:
  typedef std::complex<double> dcomplex;
  typedef Item<dcomplex> item_type;
  typedef ItemComparator<dcomplex> comparator;
  typedef std::set<item_type, comparator> grade_type;
private:
  int n_;
  std::vector<grade_type *> grade_;
  std::vector<double> thre_;
public:
  GradedComplex(int n, double *thre);
  ~GradedComplex();
  void push(item_type item);
  void avg(double *buf);
};
#endif

グレードダブル.h

#ifndef __GRADEDDOUBLE_H__
#define __GRADEDDOUBLE_H__
#include <set>
#include <vector>
#include "Item.h"
class GradedDouble
{
public:
  typedef Item<double> item_type;
  typedef ItemComparator<double> comparator;
  typedef std::set<item_type, comparator> grade_type;
private:
  int n_;
  std::vector<grade_type *> grade_;
  std::vector<double> thre_;
public:
  GradedDouble(int n, double *thre);
  ~GradedDouble();
  void push(item_type item);
  void avg(double *buf);
};
#endif

正しい SWIG インターフェイス ファイルを作成するのを手伝ってください。

4

1 に答える 1

1

GradedDoubleリンク操作でおよびGradedComplexクラスの定義が欠落しているようです。

オブジェクト ファイルの形式またはライブラリの形式で、これらの定義をリンカに提供する必要があります。

これを解決する方法についてさらに支援できるようにするには、Windows上のSWIG(Linuxでのみ使用)について十分に知りません。

于 2012-11-14T14:04:44.860 に答える