2011-12-11 31 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'。這會派上用場幾次。謝謝你提到它。 –