【问题标题】:C++ library for python - linking problems用于 python 的 C++ 库 - 链接问题
【发布时间】:2011-12-11 20:13:01
【问题描述】:

我已经编写了一个 c++ 库,我正在尝试使用 boost::python 对其进行 python 绑定。这是文件的简化版本,我在其中定义它们:

#include <boost/python.hpp>
#include <matrix.h>
#include <string>

using namespace boost::python;

class MatrixForPython : public Matrix{
  public:
    MatrixForPython(int size) : Matrix(size) {}

    std::string hello(){
      return "this method works"; 
    }
};

BOOST_PYTHON_MODULE(matrix){
  class_<MatrixForPython>("Matrix", init<int>())
    .def("hello", &MatrixForPython::hello); 
}

编译由 CMake 完成,这是我的 CMakeLists.txt:

project(SomeProject)
cmake_minimum_required(VERSION 2.8)

# find and include boost library
find_package(Boost COMPONENTS python REQUIRED)
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_PATH})
include_directories(${Boost_INCLUDE_DIR})
include_directories(.)

# compile matrix manipulation library
add_library(matrix SHARED matrix.cpp python_bindings.cpp)
set_target_properties(matrix PROPERTIES PREFIX "")
target_link_libraries(matrix
  ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})

编译完成没有错误,但是当我尝试运行简单的python脚本时:

import matrix

我收到以下错误

undefined symbol: _ZN6MatrixC2ERKS_

最让我困惑的是事实,当我将MatrixForPython 更改为不是从Matrix 派生时,一切都按预期工作。我应该改变什么才能让它发挥作用?

编辑:matrix.h

#ifndef MATRIX_H_
#define MATRIX_H_

#include <boost/shared_array.hpp>


class Matrix{
  public:
    // creates size X size matrix
    Matrix(int size);

    // copy constructor
    Matrix(const Matrix& other);

    // standard arithmetic operators
    const Matrix operator * (const Matrix& other) const;
    const Matrix operator + (const Matrix& other) const;
    const Matrix operator - (const Matrix& other) const;

    // element manipulation
    inline void set(int row, int column, double value){
      m_data[row*m_size + column] = value;
    }

    inline double get(int row, int col) const{
      return m_data[row*m_size + col];
    }

    // norms
    double frobeniusNorm() const;
    double scaledFrobeniusNorm() const;
    double firstNorm() const;
    double infNorm() const;

    // entire array manipulation
    void zero();    // sets all elements to be 0
    void one();     // identity matrix
    void hermite(); // hermite matrix


    virtual ~Matrix();


    // less typing
    typedef boost::shared_array<double> MatrixData;

  private:
    int m_size; 
    MatrixData m_data;
};




#endif

【问题讨论】:

  • 发布 matrix.h 标题
  • 您在共享库中看到 Matrix 构造函数符号了吗?
  • 是的,nm -l 显示符号 _ZN6MatrixC1Ei 正是我的构造函数在 matrix.cpp 文件中的位置。
  • 我以为你错过了_ZN6MatrixC2ERKS_ 符号?根据c++filt,它看起来像复制 ctor
  • 你是对的,在matrix.h中声明的复制构造函数没有在matrix.cpp中定义,看起来我在删除其他不必要的方法时不小心删除了它。当我实施它时,一切都开始像魅力一样工作。非常感谢您的帮助。

标签: c++ python boost cmake


【解决方案1】:

未定义符号:_ZN6MatrixC2ERKS_

这是因为您在共享库中缺少 Matrix::Matrix(const Matrix&amp;) 复制构造函数:

samm@mac ~> echo _ZN6MatrixC2ERKS_ | c++filt 
Matrix::Matrix(Matrix const&)
samm@mac ~> 

【讨论】:

  • 我从没听说过c++filt。那会派上用场几次。感谢您提及。
猜你喜欢
  • 2011-05-23
  • 2019-01-20
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 2017-11-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
相关资源
最近更新 更多