python bind函数_使用pybind11导出到python的类函数的额外打印

这是你的名字示例.cpp代码:

#include

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_(m, "mycl")

.def(py::init())

.def("AskAge",&mycl::ask_age);

}

我编译如下(这里有pybind11模块):

c++ -std=c++11 example.cpp -fPIC -shared `python3 -m pybind11 --include` -o example.so

下面是python3执行结果:

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

>>>

我不知道在哪里

13

是从哪里来的。有什么问题?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值