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

执行第二个命令后,我收到以下错误:

*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)似乎有问题我需要帮助来创建接口文件。以下是头文件

分级复杂.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

看起来链接操作缺少您GradedDoubleGradedComplex类的定义。

您必须以目标文件的形式或以库的形式将这些定义提供给链接器。

我对 Windows 上的 SWIG 了解不多(仅在 Linux 上使用),无法进一步帮助解决这个问题。

于 2012-11-14T14:04:44.860 回答