SWIG python 模块创建问题

Posted

技术标签:

【中文标题】SWIG python 模块创建问题【英文标题】:SWIG python module creation issue 【发布时间】:2012-11-14 13:49:23 【问题描述】:

我有需要在 python 中导入的 c++ 类。 为此,我正在使用 SWIG。下面是swig界面

example.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) 我需要帮助来创建接口文件。以下是头文件

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

Graded Double.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 接口文件。

【问题讨论】:

【参考方案1】:

链接操作似乎缺少GradedDoubleGradedComplex 类的定义。

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

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

【讨论】:

以上是关于SWIG python 模块创建问题的主要内容,如果未能解决你的问题,请参考以下文章

无法从使用 SWIG 创建的 python 访问 C++ 扩展模块及其方法

为 Python 或 SWIG 模块创建编译 C-dll,如何继续?

有没有办法使用 SWIG C++ 创建一个 python 模块,可以在 Python2 和 Python3 中导入

无法使用 SWIG 在 Python 中实例化 C++ 类(获取属性错误)

SWIG python初始化一个指向NULL的指针

Python 属性和 Swig