DCC Coding Session: Pybind11 and CMake

This repo contains the materials for the DCC Coding Session on Pybind11 and CMake that took place on the 14th of April 2023.

The first part of the session discusses how to combine Python and C++ using pybind11. An example of a Python wrapper for a C++ library is included in this repo on the pymymath directory.

The second part of the coding session is a quick introduction to CMake. An example of how to build a C++ application with CMake is included in the mymathapp directory of the repo.

pybind11 example

An example of a Python module that serves as a wrapper for C++ code is included in the pymymath directory. The C++ source code is located in the pymypath/mymath directory and the python bindings are in the file pymymath/src/binding.cpp.

Requirements: pip, python 3.5+ and (maybe) g++ and gcc compilers.

To build the pymymath python module:

git clone git@github.com:yiquintero/dcc-codingsession-pybind11.git
cd dcc-codingsession-pybind11
pip install ./pymymath

To use the newly created python module launch the python interpreter, import the module and call the functions.

python
>>import pymymath
>>pymymath.add(5,3)
>>8.0

CMake example

An example of a C++ app that is built with CMake is included in the mymathapp directory.

Requirements: CMake and a C++ compiler (preferably GNU for Linux or Visual Studio for Windows)

To build & run:

git clone git@github.com:yiquintero/dcc-codingsession-pybind11.git
cd dcc-codingsession-pybind11/mymathapp
mkdir build
cd build
# Configure the CMake project (CMake will check dependencies are installed, detect OS, compiler, etc.)
cmake ..
# As a result of the previous command CMake will build the build system's build files. 
# The default is Makefiles for Linux and Visual Studio project/solution files on Windows.
# On Linux, we can simply type 'make' to build the app:
make
# And to test it:
./mymathapp

Useful links

Some useful links that came up whilst preparing the contents of the session:

Python and C++ integration
pybind11
CMake
C++