2011-12-11 55 views
1

我写了一个C++库,我试图用boost :: python将python绑定到它。这简化了文件,在那里我定义它们的版本:用于python的C++库 - 链接问题

#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 
+0

发布matrix.h头文件 –

+0

你看到共享库中的Matrix构造函数符号吗? –

+0

是的,nm -l显示符号_ZN6MatrixC1Ei正好在我的构造函数在matrix.cpp文件中。 – KCH

回答

2

未定义的符号:_ZN6MatrixC2ERKS_

这是因为你缺少你的共享库的Matrix::Matrix(const Matrix&)拷贝构造函数:

[email protected] ~> echo _ZN6MatrixC2ERKS_ | c++filt 
Matrix::Matrix(Matrix const&) 
[email protected] ~> 
+0

我从来没有听说过'C++ filt'。这会派上用场几次。谢谢你提到它。 –