example:
//example.cpp
#include <pybind11/pybind11.h>
namespace py = pybind11;
int add(int i, int j) {
return i + j;
}
//example是包的名称,add是包中函数的名称
//PYBIND11_MODULE是一个宏,m是py::module类型
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function which adds two numbers");
}
>>> import example
>>> example.add(1, 2)
2.在python端为函数的指定参数赋值:
m.def("add", &add, "A function which adds two numbers", py::arg("i"), py::arg("j"));
>>> import example
>>> example.add(i=1, j=2) //此处与上方不同。
3.在python端为函数设置默认值:
int add(int i = 1, int j = 2) {
return i + j;
}m.def("add", &add, "A function which adds two numbers", py::arg("i") = 1, py::arg("j") = 2);
4.函数重载,函数参数可以为指针。
student.h #include <string> class student { public: student(std::string name,int age); student(); ~student(); int getAge(); std::string getName(); void setAge(int age); void setName(std::string name); private: std::string name_; int age_; }; student.cpp#include "student.h" student::student(std::string name, int age) { name_ = name; age_ = age; } student::student() { } student::~student() { } int student::getAge() { return age_; } std::string student::getName() { return name_; } void student::setAge(int age) { age_ = age; } void student::setName(std::string name) { name_ = name; }
school.h
#include <string> #include <vector> #include "student.h" class school { public: school(); ~school(); void setStudent(std::string name,int age); void setStudent(student* student); void getStudent(int age,student* student); void getStudent(std::string name,student* student); private: std::vector<student *> students_; }; school.cpp#include "school.h" school::school() { } school::~school() { } void school::setStudent(student* student) { students_.push_back(student); } void school::setStudent(std::string name, int age) { student* stu=new student(name,age); students_.push_back(stu); } void school::getStudent(int age, student *student) { auto stu = students_.begin(); for(;stu!=students_.end();++stu){ if((*stu)->getAge() == age){ student->setAge((*stu)->getAge()); student->setName((*stu)->getName()); } } } void school::getStudent(std::string name, student *student) { auto stu = students_.begin(); for(;stu!=students_.end();++stu){ if((*stu)->getName() == name){ student->setAge((*stu)->getAge()); student->setName((*stu)->getName()); } } }
#include <pybind11/pybind11.h> #include "student.cpp" #include "school.cpp" namespace py = pybind11; PYBIND11_MODULE(_example, m) { m.doc() = "pybind11 example plugin"; py::class_<student>(m, "student") .def(py::init<std::string,int>()) .def(py::init<>()) .def("setName", &student::setName) .def("setAge", &student::setAge) .def("getName", &student::getName) .def("getAge", &student::getAge); py::class_<school>(m, "school") .def(py::init<>()) .def("setStudent", py::overload_cast<std::string ,int>(&school::setStudent)) .def("setStudent", py::overload_cast<student*>(&school::setStudent)) .def("getStudent", py::overload_cast<int,student*>(&school::getStudent)) .def("getStudent", py::overload_cast<std::string,student*>(&school::getStudent)); }
5、虚函数绑定
class Animal { public: virtual ~Animal() { } virtual std::string go(int n_times) = 0; }; class Dog : public Animal { public: std::string go(int n_times) override { std::string result; for (int i=0; i<n_times; ++i) result += "woof! "; return result; } };PYBIND11_MODULE(example, m) { py::class_<Animal>(m, "Animal") .def("go", &Animal::go); py::class_<Dog, Animal>(m, "Dog") .def(py::init<>()); m.def("call_go", &call_go); }