Skip to content

Commit 53d30eb

Browse files
committed
cleanup the repository
1 parent d9d9f00 commit 53d30eb

File tree

18 files changed

+149
-111
lines changed

18 files changed

+149
-111
lines changed

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,24 @@ You will have to first get pybind11, compile it, and install it:
1414

1515
The pybind11 authors prefer adding pybind11 as a git sub-project: you can of course follow their instruction and adapt the `CMakeLists.txt` files in this repository.
1616

17+
## The development environment
18+
19+
You need:
20+
21+
- a development environment for C++ (g++ or clang; cmake, ...)
22+
- the Python bindings (`libpython3-dev`, `python-cxx-dev`, ...)
23+
24+
Our [first example](add/) directly uses the compiler, all other examples will need a modern version of `cmake` being installed (pybind11 should have cmake 3.4)
25+
1726
## Sample projects
1827

19-
- [`add/`](add/): the first example.
20-
- [`add-cmake`](add-cmake/): the first example with cmake
28+
- [`add/`](add/): using a c++ module to add two numbers in Python
29+
- [`add-cmake`](add-cmake/): the first example plus cmake
30+
- [`classes/`](classes/): let Python use C++ classes
31+
- [`eval-file/`](eval-file/): C++ evaluates a Python script in an external files that modifies the state of a C++ variable.
32+
- [`eval-set-object/`](eval-set-object/): Create two objects of type `Foo` and pass them to a Python script (defined as a string in the C++ code)
33+
- [`eval-set-object-module/`](eval-set-object-module/): Create a `.so` module for the `Foo` type, load it get the Python script to access it.
34+
- [`eval-file-pyqt5/`](eval-file-pyqt5/): The C++ code runs an external Python script that shows a PyQt5 alert.
2135
- ...
2236

2337
## Links
@@ -32,3 +46,26 @@ The pybind11 authors prefer adding pybind11 as a git sub-project: you can of cou
3246
## alternatives
3347

3448
- [cppy](https://pypi.python.org/pypi/cppyy) is an automatic Python-C++ bindings generator designed for large scale programs in high performance computing that use modern C++.
49+
50+
## todo
51+
52+
document all directories:
53+
54+
- [x] add
55+
- [x] add-cmake
56+
- [ ] classes
57+
- [ ] eval-file
58+
- [ ] eval-set-object
59+
- [ ] eval-set-object-module
60+
- [ ] eval-file-pyqt5
61+
- [ ] eval-file-pyqt5-set-value
62+
- [ ] scripter-api
63+
- [ ] scripter-class
64+
- [ ] scripter-class-in-class
65+
- [ ] scripting
66+
- [ ] scripting-pyqt5
67+
- [ ] qt5-pyqt5
68+
69+
open questions:
70+
71+
- [ ] how to pass sys.argv to the `QApplication()` in `eval-file-pyqt5/python/pyqt5.py`

add-cmake/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ set(PYBIND11_PYTHON_VERSION 3.4)
55

66
find_package(pybind11 CONFIG)
77

8-
include_directories(src)
9-
108
pybind11_add_module(maths
119
src/maths.cpp
1210
)

add-cmake/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# pybind11 and cmake
22

3-
Adding `cmake` to avoid the complex `g++` call in the [add example](../add).
3+
This second example shows how to use cmake to simplify the compilation of the [add example](../add).
4+
5+
Using `cmake` avoid us the use of the complex `g++` call in the [add example](../add).
46

57
Like all examples in this repository we're not binding `pybind11` as a git submodule but compile it separately in `~/bin`
68

9+
The `pybind11_DIR` passed to the cmake commands tells the compiler where to find the library.
10+
711
There is an official demo project that fetches pybind11 as a git submodule: <https://github.com/pybind/cmake_example>.
812

913
~~~.sh

add/README.md

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
11
# Add: the first example
22

3-
- install `python3-cxx-dev`
4-
- this is the command the documentation is suggesting:
3+
This first example shows how to create a python module providing the `add()` function that returns the sum of the two integers given as a parameter.
54

6-
~~~.sh
7-
c++ -O3 -shared -std=c++11 -I <path-to-pybind11>/include `python-config --cflags --ldflags` maths.cpp -o maths.so
8-
~~~
5+
This is the compile command that works for me:
6+
7+
```.sh
8+
c++ -O3 -shared -fPIC -std=c++11 -I ~/src/pybind11/include -I /usr/include/python3.5 -L /usr/lib/python3 `python-config --cflags --ldflags` maths.cpp -o maths.so
9+
```
910

10-
- using
11+
As a result ou will get the python library `maths.so`.
1112

12-
~~~.sh
13-
c++ -O3 -fPIC -std=c++11 -I <path-to-pybind11>/include `python-config --cflags --ldflags` maths.cpp -o maths.so
14-
~~~
13+
You can now import the "maths" module in Python3. Start Python3 in the directory where the file is located and:
1514

16-
i get `undefined reference to `main'`
17-
- so it gets:
15+
```.py
16+
>>> import maths
17+
>>> print(maths.add(1,2))
18+
3
19+
>>>
20+
```
1821

19-
~~~.sh
20-
c++ -O3 -fPIC -std=c++11 -I <path-to-pybind11>/include `python-config --cflags --ldflags` -c maths.cpp -o maths.so
21-
~~~
22+
# Notes
2223

23-
to compile without linking.
24-
but then, in python i get `ImportError: maths.so: only ET_DYN and ET_EXEC can be loaded` because it's not linked.
25-
we need a main, and drop the `-c`.
26-
- the compilation is working, but when importing the module, python3 complains:
24+
the pybind11 documentation is suggesting the following:
2725

28-
~~~
29-
ImportError: dynamic module does not define module export function (PyInit_maths)
30-
~~~
26+
```.sh
27+
c++ -O3 -shared -std=c++11 -I <path-to-pybind11>/include `python-config --cflags --ldflags` maths.cpp -o maths.so
28+
```
3129

32-
we were compiling with python2, but using python3. the `g++` finally is:
30+
but it fails for me.
3331

34-
~~~.sh
35-
c++ -O3 -shared -fPIC -std=c++11 -I /home/ale/src/pybind11/include -I /usr/include/python3.5 -L /usr/lib/python3 `python-config --cflags --ldflags` maths.cpp -o maths.so
36-
~~~
32+
First I need to tell the compiler to use Python3 (since Debian and many other Linux distributions still default to Python2):
33+
34+
```.sh
35+
c++ -O3 -shared -fPIC -std=c++11 -I ~/src/pybind11/include -I /usr/include/python3.5 -L /usr/lib/python3 `python-config --cflags --ldflags` maths.cpp -o maths.so
36+
```
3737

3838
(<https://docs.python.org/3/extending/embedding.html#compiling-and-linking-under-unix-like-systems>)
3939

40-
- yeah!
40+
Then I need to add a `main()` function in the `.cpp` file.
41+
42+
Question: (how) can I compile a pure library without any main?
4143

4244
## Further steps
4345

eval-file-pyqt5/CMakeLists.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@ project(scripting)
33

44
set(PYBIND11_PYTHON_VERSION 3.4)
55

6-
FIND_PACKAGE(pybind11 CONFIG)
6+
find_package(pybind11 CONFIG)
77

8-
ADD_EXECUTABLE(scripting src/main.cpp)
9-
TARGET_LINK_LIBRARIES(scripting ${PYTHON_LIBRARIES})
10-
TARGET_LINK_LIBRARIES(scripting pybind11::embed)
8+
add_executable(scripting
9+
src/main.cpp
10+
)
11+
target_link_libraries(scripting
12+
pybind11::embed
13+
)
14+
15+
configure_file(
16+
${CMAKE_CURRENT_SOURCE_DIR}/python/pyqt5.py
17+
${CMAKE_CURRENT_BINARY_DIR}
18+
COPYONLY
19+
)

eval-file-pyqt5/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Eval a python script that launches a PyQt5 dialog
22

3+
The C++ code runs an external Python script that shows a PyQt5 alert.
34

45
~~~.sh
56
$ mkdir build
7+
$ cd build
68
$ cmake -Dpybind11_DIR=/home/ale/bin/pybind11/share/cmake/pybind11
79
$ make
810
$ ./scripting

eval-file-pyqt5/pyqt5.py renamed to eval-file-pyqt5/python/pyqt5.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ def __init__(self):
1414
# app = QApplication(sys.argv)
1515
app = QApplication([])
1616
ex = App()
17+
18+

eval-file-pyqt5/src/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <iostream>
21
#include <pybind11/embed.h>
32

43
namespace py = pybind11;
@@ -9,5 +8,5 @@ int main()
98
py::scoped_interpreter guard{}; // start the interpreter and keep it alive
109

1110
py::object scope = py::module::import("__main__").attr("__dict__");
12-
py::eval_file("../pyqt5.py", scope);
11+
py::eval_file("pyqt5.py", scope);
1312
}

eval-file/CMakeLists.txt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
CMAKE_MINIMUM_REQUIRED(VERSION 3.4) # 3.4 is better for mac
2-
PROJECT(scripting)
1+
cmake_minimum_required(VERSION 3.4)
2+
project(scripting)
33

4-
SET(PYBIND11_PYTHON_VERSION 3.4)
4+
set(PYBIND11_PYTHON_VERSION 3.4)
55

6-
FIND_PACKAGE(pybind11 CONFIG)
6+
find_package(pybind11 CONFIG)
77

8-
ADD_EXECUTABLE(scripting src/main.cpp)
9-
TARGET_LINK_LIBRARIES(scripting pybind11::embed)
8+
add_executable(scripting
9+
src/main.cpp
10+
)
11+
12+
target_link_libraries(scripting
13+
pybind11::embed
14+
)
15+
16+
configure_file(
17+
${CMAKE_CURRENT_SOURCE_DIR}/python/set-the-y.py
18+
${CMAKE_CURRENT_BINARY_DIR}
19+
COPYONLY
20+
)

eval-file/README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
# Eval a python script that modifies a C++ object
22

3-
- http://pybind11.readthedocs.io/en/latest/advanced/embedding.html
4-
- https://github.com/pybind/pybind11/blob/master/tests/test_eval.cpp#L63
3+
From the C++ code we run a Python script that modifies the state of a C++ variable by calling a C++ function.
4+
5+
In the C++ program we define a `local` list that contains the variable and the function passed to the Python file when evaluating it.
56

67
~~~.sh
78
$ mkdir build
8-
$ cmake -Dpybind11_DIR=/home/ale/bin/pybind11/share/cmake/pybind11 -DPYTHON_EXECUTABLE:FILEPATH=/usr/bin/python3 ..
9+
$ cd build
10+
$ cmake -Dpybind11_DIR=~/bin/pybind11/share/cmake/pybind11 ..
911
$ make
1012
$ ./scripting
1113
~~~
14+
15+
## References
16+
17+
- http://pybind11.readthedocs.io/en/latest/advanced/embedding.html
18+
- https://github.com/pybind/pybind11/blob/master/tests/test_eval.cpp#L63

0 commit comments

Comments
 (0)