GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pybind11_cpp_python/cpp_call_python/example.cpp at master · ZQPei/pybind11_cpp_python

Expand file tree

/

example.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
33 lines (27 loc) · 838 Bytes
/

example.cpp

Copy path

File metadata and controls

33 lines (27 loc) · 838 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <pybind11/embed.h>
#include <iostream>
namespace py=pybind11;
PYBIND11_EMBEDDED_MODULE(fast_calc, m){
m.def("add", [](int i, int j){
return i+j;
});
}
int main(){
py::scoped_interpreter guard{};
// import python module
py::module m = py::module::import("human");
// Human is a python class
py::object Human = m.attr("Human");
// create a Human instance named jack
py::object jack = Human("Jack");
// call jack's methods
jack.attr("set_age")(18);
py::print(jack.attr("get_name")(),"'s age:", jack.attr("get_age")());
// automatic type convertion of python's generic types
auto fast_calc = py::module::import("fast_calc");
auto result = fast_calc.attr("add")(1,2).cast<int>();
if (result == 3){
std::cout<<true<<std::endl;
}
return 0;
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.