【问题标题】:overload operators C++ errors when compiling with SWIG使用 SWIG 编译时重载运算符 C++ 错误
【发布时间】:2015-11-18 15:27:47
【问题描述】:

我创建了这个 matrix.cpp 类,其中我重载了一些运算符:

template <typename T>
class MagicObject {
private:
    std::vector<std::vector<T> > mat;
    unsigned rows;
    unsigned cols;

public:
    MagicObject(unsigned _rows, unsigned _cols, const T& _initial);
    MagicObject(const MagicObject<T>& rhs);
    MagicObject();
    ~MagicObject();

    // Operator overloading, for "standard" mathematical MagicObject operations
    MagicObject<T>& operator=(const MagicObject<T>& rhs);

    // MagicObject mathematical operations
    MagicObject<T> operator+(const MagicObject<T>& rhs);
    MagicObject<T>& operator+=(const MagicObject<T>& rhs);
    MagicObject<T> operator-(const MagicObject<T>& rhs);
    MagicObject<T>& operator-=(const MagicObject<T>& rhs);
    MagicObject<T> operator*(const MagicObject<T>& rhs);
    MagicObject<T>& operator*=(const MagicObject<T>& rhs);
    MagicObject<T> transpose();

    // matrix/scalar operations
    MagicObject<T> operator+(const T& rhs);
    MagicObject<T> operator-(const T& rhs);
    MagicObject<T> operator*(const T& rhs);
    MagicObject<T> operator/(const T& rhs);

    // matrix/vector operations
    std::vector<T> operator*(const std::vector<T>& rhs);
    std::vector<T> diag_vec();

    // Access the individual elements
    T& operator()(const unsigned& row, const unsigned& col);
    const T& operator()(const unsigned& row, const unsigned& col) const;

    T& operator[](const unsigned &i);

    // Access the row and column sizes
    unsigned get_rows() const;
    unsigned get_cols() const;

    void _print();
};

我避免放置matrix.hpp,因为它很长。在任何情况下,标头都包含正确工作的实现(当然是在 C++ 中)。 现在我想使用 SWIG 创建一个 python 模块。为此,我创建了这个名为 matrix.i 的 SWIG 接口:

%module matrix
%{
#include "matrix.hpp"
%}

%include "matrix.hpp"

// used for the operator[]
%extend MagicObject<T> {
    T& __getitem__(unsigned int i) {
        return $self[i];
     }
}

// used for _print() function
%extend MagicObject<T> {
    void __print__() {
        print $self;
     }
}

%template(intMagicObject) MagicObject<int>;
%template(floatMagicObject) MagicObject<float>;
%template(doubleMagicObject) MagicObject<double>;

我使用这些命令来创建模块:

swig -c++ -python matrix.i
g++ -O2 -fPIC -c matrix.cpp
g++ -O2 -fPIC -c matrix_wrap.cxx -I/Users/dado/anaconda/include/python2.7
g++ -lpython -dynamclib matrix.o matrix_wrap.o -o _matrix.so

当我执行最后一个命令时出现这些错误:

clang: warning: argument unused during compilation: '-dynamclib'
Undefined symbols for architecture x86_64:
  "MagicObject<double>::MagicObject()", referenced from:
      _wrap_new_doubleMagicObject(_object*, _object*) in matrix_wrap.o
      _wrap_doubleMagicObject_transpose(_object*, _object*) in matrix_wrap.o
      _wrap_doubleMagicObject___add__(_object*, _object*) in matrix_wrap.o
      _wrap_doubleMagicObject___sub__(_object*, _object*) in matrix_wrap.o
      _wrap_doubleMagicObject___div__(_object*, _object*) in matrix_wrap.o
      _wrap_doubleMagicObject___mul__(_object*, _object*) in matrix_wrap.o
  "MagicObject<float>::MagicObject()", referenced from:
      _wrap_new_floatMagicObject(_object*, _object*) in matrix_wrap.o
      _wrap_floatMagicObject_transpose(_object*, _object*) in matrix_wrap.o
      _wrap_floatMagicObject___add__(_object*, _object*) in matrix_wrap.o
      _wrap_floatMagicObject___sub__(_object*, _object*) in matrix_wrap.o
      _wrap_floatMagicObject___div__(_object*, _object*) in matrix_wrap.o
      _wrap_floatMagicObject___mul__(_object*, _object*) in matrix_wrap.o
  "MagicObject<int>::MagicObject()", referenced from:
      _wrap_new_intMagicObject(_object*, _object*) in matrix_wrap.o
      _wrap_intMagicObject_transpose(_object*, _object*) in matrix_wrap.o
      _wrap_intMagicObject___add__(_object*, _object*) in matrix_wrap.o
      _wrap_intMagicObject___sub__(_object*, _object*) in matrix_wrap.o
      _wrap_intMagicObject___div__(_object*, _object*) in matrix_wrap.o
      _wrap_intMagicObject___mul__(_object*, _object*) in matrix_wrap.o
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我该如何解决这个问题?谢谢

编辑这里是我的 matrix.cpp 文件(只是一个 sn-p):

#include "matrix.hpp"
#include <iostream>

// Parameter Constructor
template<typename T>
MagicObject<T>::MagicObject(unsigned _rows, unsigned _cols, const T& _initial) {
    mat.resize(_rows);
    for (unsigned i=0; i<mat.size(); i++) {
        mat[i].resize(_cols, _initial);
    }
    rows = _rows;
    cols = _cols;
}

// Copy Constructor
template<typename T>
MagicObject<T>::MagicObject(const MagicObject<T>& rhs) {
    mat = rhs.mat;
    rows = rhs.get_rows();
    cols = rhs.get_cols();
}

// (Virtual) Destructor
template<typename T>
MagicObject<T>::~MagicObject() {}

// Assignment Operator
template<typename T>
MagicObject<T>& MagicObject<T>::operator=(const MagicObject<T>& rhs) {
    if (&rhs == this)
        return *this;

    unsigned new_rows = rhs.get_rows();
    unsigned new_cols = rhs.get_cols();

    // ..... rest here

【问题讨论】:

  • 您显示的链接器错误看起来与您没有在库中的任何位置正确实例化 C++ 中的模板完全一样,但是由于您没有将问题简化为最小的完整示例,因此我无法进一步分析,但可能您根本没有给出默认构造函数的 定义 并且到目前为止还没有发现它。见stackoverflow.com/questions/495021/…
  • %extend 中的 print $self 也是错误的,并且应该在出现语法错误的链接器错误之前很久就失败了。
  • @Flexo 对不起,我不太明白你的意思。我编辑了帖子,也许你可以给我一个例子。
  • 我猜你没有在 matrix.hpp 的任何地方定义MagicObject::MagicObject。要么不声明它,要么在适当的地方提供定义。

标签: python c++ operators swig


【解决方案1】:

您需要在 .cpp 或 .h 文件中实例化您的模板类,就像,

template class yourclass< double >;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    • 2019-06-27
    • 1970-01-01
    相关资源
    最近更新 更多