Giter VIP home page Giter VIP logo

cppwg's Introduction

build

cppwg

Automatically generate PyBind11 Python wrapper code for C++ projects.

Installation

Clone the repository and install cppwg:

git clone https://github.com/Chaste/cppwg.git
cd cppwg
pip install .

Usage

This project generates PyBind11 wrapper code, saving lots of boilerplate in bigger projects. Please see the PyBind11 documentation for help on the generated wrapper code.

First Example

The examples/shapes/ directory is a full example project, demonstrating how to generate a Python package pyshapes from C++ source code. It is recommended that you use it as a template project when getting started.

As a small example, we can start with a free function in examples/shapes/src/math_funcs/SimpleMathFunctions.hpp:

#ifndef _SIMPLEMATHFUNCTIONS_HPP
#define _SIMPLEMATHFUNCTIONS_HPP

/**
 * Add the two input numbers and return the result
 * @param i the first number
 * @param j the second number
 * @return the sum of the numbers
 */
int add(int i, int j)
{
    return i + j;
}

#endif  // _SIMPLEMATHFUNCTIONS_HPP

Add a package description to examples/shapes/wrapper/package_info.yaml:

name: pyshapes
modules:
- name: math_funcs
  free_functions: CPPWG_ALL

Generate the wrappers with:

cd examples/shapes
cppwg src/ \
  --wrapper_root wrapper/ \
  --package_info wrapper/package_info.yaml \
  --includes src/math_funcs/

The following PyBind11 wrapper code will be output to examples/shapes/wrapper/math_funcs/math_funcs.main.cpp:

#include <pybind11/pybind11.h>
#include "wrapper_header_collection.hpp"

namespace py = pybind11;

PYBIND11_MODULE(_pyshapes_math_funcs, m)
{
    m.def("add", &add, "");
}

The wrapper code can be built into a Python module and used as follows:

from pyshapes import math_funcs
a = 4
b = 5
c = math_funcs.add(4, 5)
print c
>>> 9

Full Example

To generate Pybind11 wrappers for all the C++ code in examples/shapes:

cd examples/shapes
cppwg src/ \
  --wrapper_root wrapper/ \
  --package_info wrapper/package_info.yaml \
  --includes src/geometry/ src/math_funcs/ src/mesh/ src/primitives

To build the example pyshapes package:

mkdir build
cd build
cmake ..
make

Starting a New Project

  • Make a wrapper directory in your source tree e.g. mkdir wrappers
  • Copy the template in examples/shapes/wrapper/generate.py to the wrapper directory and fill it in as appropriate.
  • Copy the template in examples/shapes/wrapper/package_info.yaml to the wrapper directory and fill it in as appropriate.
  • Run cppwg with appropriate arguments to generate the PyBind11 wrapper code in the wrapper directory.
  • Follow the PyBind11 guide for building with CMake, using examples/shapes/CMakeLists.txt as an initial guide.

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.