Getting ready

We will keep account.cpp unchanged with respect to the previous two recipes and only modify account.hpp:

#pragma once#include <pybind11/pybind11.h>class Account {public:  Account();  ~Account();  void deposit(const double amount);  void withdraw(const double amount);  double get_balance() const;private:  double balance;};namespace py = pybind11;PYBIND11_MODULE(account, m) {  py::class_<Account>(m, "Account")      .def(py::init())      .def("deposit", &Account::deposit)      .def("withdraw", &Account::withdraw)      .def("get_balance", &Account::get_balance);}

We will follow the pybind11 documentation "Building with CMake" (https://pybind11.readthedocs.io/en/stable/compiling.html#building-with-cmake) and introduce the pybind11 CMake code using add_subdirectory ...

Get CMake Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.