Boost Python:从多个.cpp文件中创建一个模块(.so)

时间:2016-01-05 00:33:18

标签: python c++ boost

我开始编写(在C ++中)一些我希望在Python代码中可调用的模块。我为此目的使用Boost Python。随着我的代码将不断增长,我决定将其分成几个.cpp文件。现在,我有类似的东西:
Foo.cpp中

#include "Bar.hpp"
#include "Baz.hpp"
#include <boost/python.hpp>

using namespace boost::python;


BOOST_PYTHON_MODULE(foo){

    def("help", help);

    class_<Bar>("Bar", init<bp::list>())
    .def("add", &Bar::add)
    .def("show", &Bar::show);


}

Bar.cpp

#include <boost/python.hpp>
#include <string>
#include <vector>
#include "Baz.hpp"

namespace bp=boost::python;


class Bar{
    private:
        std::vector<std::string> items; 

    public:
        Bar(bp::list list):items(toCppVector(list)){}
        ~Bar(){}
        void add(std::string x){
            numbers.push_back(x);
        }
        bp::list show(){
            return toPythonList(items);
        }           
};

Bar.hpp

#ifndef Bar_hpp
#define Bar_hpp
#include <boost/python.hpp>
#include <vector>
#include <string>
namespace bp=boost::python;

class Bar{
    private:
        std::vector<std::string> items; 

    public:
        Bar(bp::list list);
        ~Bar();
        void add(std::string x);
        bp::list show();        
};

#endif

Baz.cpp

#include <boost/python.hpp>
#include <string>
#include <vector>

namespace bp=boost::python;


//C++ vector to Python list
template <class T>
bp::list toPythonList(std::vector<T> vector) {
    typename std::vector<T>::iterator iter;
    bp::list list;
    for (iter = vector.begin(); iter != vector.end(); ++iter) {
        list.append(*iter);
    }
    return list;
}

//Python list to C++ int vector
std::vector<int> toCppVectorInt(bp::list list){
    std::vector<int> vector;
    for (int i=0;i !=bp::len(list) ;i++){
        vector.push_back(bp::extract<int>((list)[i]));
    }
    return vector;
}

//Python list to C++ string vector
std::vector<std::string> toCppVectorStr(bp::list list){
    std::vector<std::string> vector;
    for (int i=0;i !=bp::len(list) ;i++){
        vector.push_back(bp::extract<std::string>((list)[i]));
    }
    return vector;
}

std::string help(){
    return "some help information ";    

}

Baz.hpp

#ifndef Baz_hpp
#define Baz_hpp
#include <boost/python.hpp>
#include <vector>
#include <string>

namespace bp=boost::python;
template <class T>
bp::list toPythonList(std::vector<T> vector);
//Python list to C++ int vector
std::vector<int> toCppVectorInt(bp::list list);
//Python list to C++ string vector
std::vector<std::string> toCppVectorStr(bp::list list);
std::string help();

#endif

和我的 setup.py 文件:

from distutils.core import setup
from distutils.extension import Extension

setup(name="PackageName", ext_modules=[Extension("foo",["foo.cpp"],
        libraries = ["boost_python"])
    ])

与之匹配:

python setup.py build 

我获取文件foo.so(编译终止,没有错误)。不幸的是,它不起作用。当我试图在Python解释器中使用它时,我收到了这个效果:

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: ./foo.so: undefined symbol: _ZN3NNB3addESs
>>> 

我的代码应该更正?

0 个答案:

没有答案