This recipe builds on top of Chapter 9, Mixed-language Projects, Recipe 5, Building C++ and Python projects using pybind11. Let us see how in detail:
First, we extend account/CMakeLists.txt. The only addition is the last directive, which specifies the install target:
install(
TARGETS
account
LIBRARY
DESTINATION account
)
And that's it! With the install target and the README.rst, MANIFEST.in, setup.py, __init__.py, and version.py files in place, we are ready to test the installation of our example code which is interfaced using pybind11:
- For this, create a new directory somewhere on your computer and we will test the installation there.
- Inside the newly created directory, we run pipenv install from a local path. Adjust the local path to point to the directory that holds the setup.py script:
$ pipenv install /path/to/cxx-example
- Now we spawn a Python shell inside the Pipenv environment:
$ pipenv run python
- Inside the Python shell, we can test our CMake package:
>>> from account import Account
>>> account1 = Account()
>>> account1.deposit(100.0)
>>> account1.deposit(100.0)
>>> account1.withdraw(50.0)
>>> print(account1.get_balance())
150.0