我有在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界面文件。
答案 0 :(得分:1)
看起来链接操作缺少GradedDouble
和GradedComplex
类的定义。
您必须以对象文件的形式或以库的形式向链接器提供这些定义。
我对Windows上的SWIG知之甚少(仅在Linux上使用它)能够进一步帮助解决这个问题。