【问题标题】:Boost Python and C++ header filesBoost Python 和 C++ 头文件
【发布时间】:2012-08-21 09:13:54
【问题描述】:

我想为我的C++ shared library 提供python 接口,我打算同样使用Boost::python,我的基于C++ 的代码非常庞大,并且分为headersimplementation 文件。但是所有python::boost 示例都讨论了在cpp 文件中添加python::boost 结构以及如何处理头文件。我可以基于相同的代码构建C++ 共享库和python modules

【问题讨论】:

    标签: c++ python boost boost-python


    【解决方案1】:

    这里是如何使用 Boost.Python 的指南的链接:http://www.boost.org/doc/libs/1_51_0/libs/python/doc/tutorial/doc/html/python/exposing.html

    使用标头/实现结构的示例:

    //hpp
    #include <boost/python.hpp>
    using namespace boost::python;
    
    struct World
    {
        void set(std::string msg);
        std::string greet();
        std::string msg;
    };
    
    BOOST_PYTHON_MODULE(hello)
    {
        class_<World>("World")
            .def("greet", &World::greet)
            .def("set", &World::set)
        ;
    }
    
    //cpp
    
    void World::set(std::string msg) { this->msg = msg; }
    std::string greet() { return this->msg; }
    

    【讨论】:

    • ok,所以不用担心实现文件,只需要正确暴露头文件中的方法!!!
    • @Avinash:是的,有关详细信息,请参阅我提供的链接。有些类型可以自动公开(例如 int、std::string),有些类型必须由您公开。自定义类型(例如 MyCustomContainer)始终需要由您公开。
    • 这是世界::greet()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    相关资源
    最近更新 更多