Skip to content

Commit 9e1987a

Browse files
committed
pybind.cpp
1 parent b00a8b1 commit 9e1987a

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

section4/demo.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/python3
2+
3+
import pydemo
4+
5+
x = pydemo.add(1,2)
6+
print(x)

section4/pybind.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2020 by Chrono
2+
//
3+
// apt install python3-pip
4+
// pip3 install pybind11
5+
//
6+
// apt install python3-dev
7+
// mkdir build && cd build
8+
// cmake .. -DPYBIND11_TEST=OFF
9+
//
10+
// g++ -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` pybind.cpp -o pydemo`python3-config --extension-suffix`
11+
12+
#include <pybind11/pybind11.h>
13+
14+
int add(int a, int b)
15+
{
16+
return a + b;
17+
}
18+
19+
PYBIND11_MODULE(pydemo, m)
20+
{
21+
m.doc() = "pybind11 doc";
22+
m.def("add", &add, "add func");
23+
}
24+
25+
#if 0
26+
27+
#include <iostream>
28+
29+
int main()
30+
{
31+
using namespace std;
32+
33+
cout << "pybind demo" << endl;
34+
}
35+
#endif

0 commit comments

Comments
 (0)