Thanks to visit codestin.com
Credit goes to github.com

Skip to content
/ cppwg Public
forked from Chaste/cppwg

An automatic wrapper generator for C++

License

termi3/cppwg

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cppwg

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

Example

cppwg/shapes/ 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 cppwg/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 cppwg/shapes/wrapper/package_info.yaml:

name: pyshapes
modules:
- name: math_funcs
  free_functions: cppwg_ALL

and do python cppwg/shapes/wrapper/generate.py (with some suitable arguements).

The generator will make the following PyBind wrapper code in cppwg/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, "");
}

which can be built into a Python module and (with some import tidying) used as follows:

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

Usage

It is recommended that you learn how to use PyBind11 first. This project just generates PyBind11 wrapper code, saving lots of boilerplate in bigger projects.

Dependencies

Developed and tested with Python 3 on Ubuntu 18.04.

  • Download the CastXML binary (available here)
  • Install Python dependencies pip install pygccxml pyyaml
  • Clone cppwg with git clone https://github.com/jmsgrogan/cppwg.git

Test the Installation

To generate the full pyshapes wrapper do:

git clone https://github.com/jmsgrogan/cppwg.git $CPPWG_DIR
python $CPPWG_DIR/shapes/wrapper/generate.py --source_root $CPPWG_DIR/shapes/src/ --wrapper_root $CPPWG_DIR/shapes/wrapper/ --castxml_binary castxml/bin/castxml --package_info $CPPWG_DIR/shapes/wrapper/package_info.yaml --includes $CPPWG_DIR/shapes/src/

then to build the example package do:

mkdir $BUILD_DIR
cd $BUILD_DIR
cmake $CPPWG_DIR/shapes
make

To test the resulting package do:

python test_functions.py 
python test_classes.py 

Starting a New Project

  • Make a wrapper directory in your source tree mkdir $WRAPPER_DIR
  • Copy the template in cppwg\shapes\wrapper\generate.py to $WRAPPER_DIR and fill it in.
  • Copy the template in cppwg\shapes\wrapper\package_info.yaml to $WRAPPER_DIR and fill it in.
  • Do python $WRAPPER_DIR\generate.py to generate the PyBind11 wrapper code in $WRAPPER_DIR.
  • Follow the PyBind11 guide for building with CMake, using cppwg\shapes\CMakeLists.txt as an initial guide.

About

An automatic wrapper generator for C++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 80.9%
  • Python 13.4%
  • CMake 5.3%
  • Other 0.4%