Python 调用 C++,python调用,#include
#include <boost/python.hpp>#include <boost/python/module.hpp>#include <boost/python/def.hpp>#include <boost/python/to_python_converter.hpp>#includeusing namespace std;using namespace boost::python;namespace HelloPython{// 简单函数char const* sayHello(){    return "Hello from boost::python";}// 简单类class HelloClass{public:    HelloClass(const string&amp; name):name(name){    }public:    string sayHello(){      return "Hello from HelloClass by : " + name;    }private:    string name;};// 接受该类的简单函数string sayHelloClass(HelloClass&amp; hello){    return hello.sayHello() + " in function sayHelloClass";}//STL容器typedef vector<int> ivector;//有默认参数值的函数void showPerson(string name,int age=30,string nationality="China"){    cout << name << " " << age << " " << nationality << endl;}// 封装带有默认参数值的函数BOOST_PYTHON_FUNCTION_OVERLOADS(showPerson_overloads,showPerson,1,3) //1:最少参数个数,3最大参数个数// 封装模块BOOST_PYTHON_MODULE(HelloPython){    // 封装简单函数    def("sayHello",sayHello);    // 封装简单类,并定义__init__函数    class_("HelloClass",init())      .def("sayHello",&amp;HelloClass::sayHello)//Add a regular member function      ;    def("sayHelloClass",sayHelloClass); // sayHelloClass can be made a member of module!!!    // STL的简单封装方法    class_("ivector")      .def(vector_indexing_suite());    class_ >("ivector_vector")      .def(vector_indexing_suite >());    // 带有默认参数值的封装方法    def("showPerson",showPerson,showPerson_overloads());}

评论关闭