|
1 | 1 | # Add: the first example |
2 | 2 |
|
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. |
5 | 4 |
|
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 | +``` |
9 | 10 |
|
10 | | -- using |
| 11 | +As a result ou will get the python library `maths.so`. |
11 | 12 |
|
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: |
15 | 14 |
|
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 | +``` |
18 | 21 |
|
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 |
22 | 23 |
|
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: |
27 | 25 |
|
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 | + ``` |
31 | 29 |
|
32 | | - we were compiling with python2, but using python3. the `g++` finally is: |
| 30 | +but it fails for me. |
33 | 31 |
|
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 | + ``` |
37 | 37 |
|
38 | 38 | (<https://docs.python.org/3/extending/embedding.html#compiling-and-linking-under-unix-like-systems>) |
39 | 39 |
|
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? |
41 | 43 |
|
42 | 44 | ## Further steps |
43 | 45 |
|
|
0 commit comments