Here is the example.cpp code :
#include <pybind11/pybind11.h>
namespace py = pybind11;
int add(int i, int j) {
return i + j;
}
class mycl{
public:
mycl(){};
mycl(int a){
age = a;
};
void call(){
printf("I'm here!\n");
}
int ask_age(){
printf("my age is %d\n", age);
}
private:
int age;
};
class mycl myclv;
class mycl *pmycl = &myclv;
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
using namespace pybind11::literals;
m.def("add", &add, //"A function which adds two numbers",
"i"_a, "j"_a);
//py::arg("i"), py::arg("j"));
py::object omycl = py::cast("Pointer to mycl");
m.attr("ptr") = omycl;
py::class_<mycl>(m, "mycl")
.def(py::init<int>())
.def("AskAge",&mycl::ask_age);
}
I compiled this as below (I have pybind11 module here) :
c++ -std=c++11 example.cpp -fPIC -shared `python3 -m pybind11 --include` -o example.so
Here is the python3 execution result :
ckim@chan-ubuntu:~/PYBIND11/pybind11$ python3
Python 3.5.2 (default, Jul 17 2020, 14:04:10)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
>>> a = example.mycl(53)
>>> a.AskAge()
my age is 53
13
>>> b = example.mycl(54)
>>> b.AskAge()
my age is 54
13
>>>
I don't know where the 13
is coming from. What is the problem?
Aucun commentaire:
Enregistrer un commentaire