Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andreibarsan/python-bindings
Simple CMake + boost-python + NumPy project sample.
https://github.com/andreibarsan/python-bindings
Last synced: 20 days ago
JSON representation
Simple CMake + boost-python + NumPy project sample.
- Host: GitHub
- URL: https://github.com/andreibarsan/python-bindings
- Owner: AndreiBarsan
- Created: 2020-02-24T03:53:49.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-24T04:39:44.000Z (almost 5 years ago)
- Last Synced: 2023-03-15T23:10:18.964Z (almost 2 years ago)
- Language: C++
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Mini boost-python CMake project
A simple project skeleton which can build native C++ code using CMake and boost-python and pass NumPy arrays back and forth. I wrote this in Feb 2020 to teach myself the basics of boost-python.
The most key elements are in `native/CMakeLists.txt` and `native/__init__.py`.
There are probably better ways of organizing the code (cf. the Pytorch source code, for instance), but this should work OK for smaller projects.
## Minimal Instructions
0. Install `cmake` (>=2.8) and `libbost-python`, `libboost-numpy` as well as a modern Python with NumPy.
1. Build the native code.
(You may need to explicily point to the proper Python installation if you're using, e.g., Anaconda and the build fails or the code crashes (pass `-DPYTHON_EXECUTABLE=... -DPYTHON_INCLUDE_DIR=... -DPYTHON_LIBRARY=...` to `cmake`).)
```bash
mkdir build && cd $_ && cmake ..
```
2. From the project root, run `main.py` with the same Python you built against. You should see something like:
```
Calling native function...
Hello from C++
13
--------------------------------------------------------------------------------
Calling helper function from binding module...
Hello, I'm a Python helper...
Hello from C++
13
--------------------------------------------------------------------------------
(Python) Calling native module with np array of shape: ((5, 5))
(C++) Old shape: [5, 5]
(C++) Dtype: float64
(C++) New shape: [25, 1]
(Python) Resulting shape: (25, 1)
```
This means the code is now able to pass arrays back and forth between Python and C++!