Giter VIP home page Giter VIP logo

pybind_stl_vector_example's Introduction

Wrap a C++ library using Pybind11

This is a minimalist working example of building a python module to wrap a custom C++ library which uses the STL (vector) and template classes/functions using PyBind11.

Feel free to fork this project to start a new one or add any of the files to your project.

Project tree

/
|- pybind/                      # The Pybind library (as git submodule)
|- src/
    |- mylib.*                  # The custom library
    |- pybind_def.cpp           # Pybind glue code
    |- test.cpp                 # simple C++ test of the library
|- CMakeLists.txt               # CMake configuration file
|- setup.py                     # Python install instructions
|- README.md                    # This file

explication

In order to build your python module you need the pybind11 folder (from the PyBind github directly). Pybind is included here as a git submodule, therefore you have 2 options for cloning this repo (and get pybind11):

  1. use directly: git clone --recursive https://github.com/mpascucci/pybind_stl_vector_example.git the first time you clone this repo.

  2. clone normally and then run git submodule init and git submodule update to get pybind11.

Next, you have to do is edit the following files for your project's needs.

  • CMakeLists.txt

  • setup.py

  • src/pybind_def.cpp

Specify the Python Interpreter

If you want to compile the python module for a particular python interpter installed on your system, you can specify it by editing this line in the CmakeLists.txt file: set(PYTHON_EXECUTABLE "~/miniconda3/bin/python3")

Build

This project is based on this Pybind-Cmake example.

Go to the source folder and run the command

$ pip install -e .

This will compile and generate a .so importable python module in the project folder.

If you want to install the module in your system instead, run:

$ pip install .

Test

Run the test.py python script (using the same interpreter if you specified one).

Alternatively tun a python session in the project root:

$ python
>>> import lib_example
>>> import numpy as np
>>> v = np.arange(10)
>>> print(lib_example.sum_vector(v))
45

pybind_stl_vector_example's People

Contributors

mpascucci avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.