gpt4 book ai didi

python 的 C++ 库 - 链接问题

转载 作者:行者123 更新时间:2023-11-30 04:30:48 25 4
gpt4 key购买 nike

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

#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

最佳答案

undefined symbol: _ZN6MatrixC2ERKS_

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

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

关于python 的 C++ 库 - 链接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8467102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com