Skip to content

Commit fdc1e56

Browse files
committed
pybind11 cpp python
0 parents  commit fdc1e56

48 files changed

Lines changed: 1071 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
724 Bytes
Binary file not shown.

cpp_call_python/compile.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
g++ -std=c++11 example.cpp -o example -I/home/pzq/anaconda3/include/python3.6m -I/usr/local/include -L/home/pzq/anaconda3/lib -lpython3.6m

cpp_call_python/example

569 KB
Binary file not shown.

cpp_call_python/example.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <pybind11/embed.h>
2+
#include <iostream>
3+
4+
namespace py=pybind11;
5+
6+
PYBIND11_EMBEDDED_MODULE(fast_calc, m){
7+
m.def("add", [](int i, int j){
8+
return i+j;
9+
});
10+
}
11+
12+
int main(){
13+
py::scoped_interpreter guard{};
14+
15+
// import python module
16+
py::module m = py::module::import("human");
17+
// Human is a python class
18+
py::object Human = m.attr("Human");
19+
// create a Human instance named jack
20+
py::object jack = Human("Jack");
21+
// call jack's methods
22+
jack.attr("set_age")(18);
23+
py::print(jack.attr("get_name")(),"'s age:", jack.attr("get_age")());
24+
25+
// automatic type convertion of python's generic types
26+
auto fast_calc = py::module::import("fast_calc");
27+
auto result = fast_calc.attr("add")(1,2).cast<int>();
28+
if (result == 3){
29+
std::cout<<true<<std::endl;
30+
}
31+
32+
return 0;
33+
}

cpp_call_python/human.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Human(object):
2+
def __init__(self, name):
3+
self.name = name
4+
5+
def set_age(self, age):
6+
self.age = age
7+
8+
def get_name(self):
9+
return self.name
10+
11+
def get_age(self):
12+
return self.age
346 Bytes
Binary file not shown.

cpp_call_python_numpy/algo.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import numpy as np
2+
3+
def det(a):
4+
return np.linalg.det(a)
5+
6+
def inv(a):
7+
return np.linalg.inv(a)

cpp_call_python_numpy/compile.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
g++ -std=c++11 example.cpp -o example -I/home/pzq/anaconda3/include/python3.6m -I/usr/local/include -I/usr/local/include/eigen3 -L/home/pzq/anaconda3/lib -lpython3.6m

cpp_call_python_numpy/example

1.15 MB
Binary file not shown.

cpp_call_python_numpy/example.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <pybind11/embed.h>
2+
#include <pybind11/eigen.h>
3+
#include <Eigen/LU>
4+
#include <iostream>
5+
6+
namespace py = pybind11;
7+
8+
int main(){
9+
py::scoped_interpreter guard{};
10+
11+
// you should have eigen installed first
12+
py::module m = py::module::import("algo");
13+
Eigen::MatrixXd a = Eigen::MatrixXd::Random(3,3);
14+
std::cout<<"a[0,0] = "<<a(0,0)<<std::endl;
15+
std::cout<<"det of a = "<<a.determinant()<<std::endl;
16+
std::cout<<"inv of a = "<<a.inverse()<<std::endl;
17+
18+
// in pybind11, eigen MatrixXd type will convert to python numpy ndarray automaticly
19+
py::print("det of a =",m.attr("det")(a));
20+
py::print(m.attr("inv")(a));
21+
22+
return 0;
23+
}

0 commit comments

Comments
 (0)