python - swig 构建问题 - python

标签 python swig

我有 C++ 类,需要在 python 代码中使用它。为此,使用 swig 生成包装类。根据文档配置了 example.i

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

%{
#define SWIG_FILE_WITH_INIT
#include "item.h"
#include "GradedComplex.h"
#include "GradedDouble.h"
%}

%include "item.h"
%include "GradedComplex.h"
%include "GradedDouble.h"

我正在尝试在 Windows 中使用 swig 构建一个包装类

c:>swig -c++ -python example.i

c:>python setup.py build_ext --inplace

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

example_wrap.cxx
C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\xlocale(342) : warning C
4530: C++ exception handler used, but unwind semantics are not enabled. Specify
/EHsc
c:\documents and settings\swig\item.h(73) : warning C45
21: 'Item<std::complex<double>>' : multiple copy constructors specified
c:\documents and settings\swig\item.h(57) : error C2106
: '+=' : left operand must be l-value
c:\documents and settings\swig\item.h(58) : error C2106
: '+=' : left operand must be l-value
c:\documents and settings\swig\item.h(63) : error C2106
: '-=' : left operand must be l-value
c:\documents and settings\swig\item.h(64) : error C2106
: '-=' : left operand must be l-value
c:\documents and settings\swig\item.h(69) : error C2106
: '=' : left operand must be l-value
c:\documents and settings\swig\item.h(70) : error C2106
: '=' : left operand must be l-value
example_wrap.cxx(3275) : error C2512: 'Item<std::complex<double>>' : no appropriate default constructor available

c:\documents and settings\swig\Item.h(38) : warning C45
21: 'Item<T>' : multiple copy constructors specified
        with
        [
            T=double
        ]
        example_wrap.cxx(3425) : see reference to class template instantiation '
Item<T>' being compiled
        with
        [
            T=double
        ]
error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe"' fa
iled with exit status 2

item.h如下

#ifndef __ITEM_H__
#define __ITEM_H__

#include <complex>
#include <functional>
#include <string>

template<typename T>
class Item
{
  std::string name_;
  T val_;

public:
  Item(std::string name, T val) : name_(name), val_(val) {}
  Item(Item<T> &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  Item(const Item<T> &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  ~Item() {}

  std::string name() const { return name_; }
  T operator()() const { return val_; }
  double norm() const { return sqrt(val_ * val_); }
  Item<T> &operator+=(Item<T> &rhs)
  {
    val_ += rhs();
    return *this;
  }
  Item<T> &operator-=(Item<T> &rhs)
  {
    val_ -= rhs();
    return *this;
  }
  Item<T> &operator*=(Item<T> &rhs)
  {
    val_ *= rhs();
    return *this;
  }
};

template<>
class Item<std::complex<double> >
{
  std::string name_;
  std::complex<double> val_;

public:
  Item(std::string name, std::complex<double> val) : name_(name), val_(val) {}
  Item(Item<std::complex<double> > &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  Item(const Item<std::complex<double> > &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  ~Item() {}

  std::string name() const { return name_; }
  std::complex<double> operator()() const { return val_; }
  double norm() const { return sqrt(val_.real() * val_.real() + val_.imag() * val_.imag()); }
  Item<std::complex<double> > &operator+=(Item<std::complex<double> > &rhs)
  {
    val_.real() += rhs().real();
    val_.imag() += rhs().imag();
    return *this;
  }
  Item<std::complex<double> > &operator-=(Item<std::complex<double> > &rhs)
  {
    val_.real() -= rhs().real();
    val_.imag() -= rhs().imag();
    return *this;
  }
  Item<std::complex<double> > &operator*=(Item<std::complex<double> > &rhs)
  {
    val_.real() = val_.real() * rhs().real() - val_.imag() * rhs().imag();
    val_.imag() = val_.real() * rhs().imag() + val_.imag() * rhs().real();
    return *this;
  }
};

template<typename T>
struct ItemComparator : public std::binary_function<Item<T>, Item<T>, bool>
{
  inline bool operator()(Item<T> lhs, Item<T> rhs)
  {
    return lhs.norm() < rhs.norm();
  }
};

#endif

最佳答案

您的 C++ 代码中有错误。 std::complex::real() 不返回左值,因此无法编译:

  Item<std::complex<double> > &operator+=(Item<std::complex<double> > &rhs)
  {
    val_.real() += rhs().real();
    val_.imag() += rhs().imag();
    return *this;
  }

你可以使用这个:

val_ += rhs();

关于python - swig 构建问题 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13305101/

相关文章:

python - 来自flask的响应在React前端收到的Json上有额外的键

使用 SWIG 的 Python C 扩展(使用 %pythoncode 关键字添加魔术方法)

python - SWIG 在 Windows 中生成 C++ Python3 包装器导致断言 MSVC 2017

python - Swig C++ 到 python : compiling a bunch of . cpp 和 .h 文件

Java JNI 在某些具有特定签名的 C 函数上崩溃

Python 导出一些列表并将所有列表放入 for 循环中

python - 如何在python中执行shell命令?

python - 错误 : variable has incomplete type 'void'

python - 相同的模式但不同的结果

javaout 类型映射不适用于 std::vector