Python Bindings for C++

This repository demonstrates how to use pybind11 to build Python modules out of C++ code so it can be used in Python like a “normal” Python module.

You can see our example libs/src_cxx/. To make the Python bindings you can simply perform

$ cmake -S ./ -B ./build
$ cd build && make

which fetches and uses pybind11 (see CMakeLists.txt) to make a Python module out of the C++ code.

Deleting C++ From This Repository

Not using C++? Don’t want it in your repository? Then you can remove doxygen, breathe and certain files from your repository:

Hint: After you have successfully deleted the C++ parts of the repository, you can make the documentation without them, e.g. cd ./docs && mkdir build && make html.

To delete the C++ parts of the repository:

  1. Delete the files/directories:

    $ rm -rf libs/src_cxx docs/source/src_cxx docs/doxygen CMakeLists.txt tests/test_mock_cxx.py
    
  2. In the file docs/source/index.rst delete: src_cxx/index from the toctree.

  3. In the file .github/workflows/CI.yaml delete:

    - name: Build Pybind11 module for C++ code
      run: |
        cmake -S ./ -B ./build
        cd build && make
    

    and

    - name: Install Doxygen
      run: /usr/share/miniconda/envs/goodsciproj_env/bin/doxygen --version
      shell: bash
    - name: Generate Doxygen Documentation
      run: |
        cd docs &&
        mkdir build &&
        mkdir build/doxygen &&
        /usr/share/miniconda/envs/goodsciproj_env/bin/doxygen doxygen/doxygen.dox
      shell: bash
    
  4. In docs/source/conf.py delete "breathe" from extenstions and

    # Integrate doxygen with sphinx via breathe
    breathe_projects = {
        "src_cxx": "../build/doxygen/xml/",
    }
    
    breathe_default_project = "proj"
    
  5. In .pre-commit-config.yaml delete:

    -   repo: https://gitlab.com/daverona/pre-commit/cpp
        rev: 0.8.0
        hooks:
        -   id: cpplint
            args: [--linelength=100, "--filter=-runtime/references,-readability/braces,-build/include,-build/c++11"]
            types_or: [c, c++, cuda]
    
  6. In requirements.txt delete breathe.

  7. In environment.yaml delete - conda-forge::doxygen>=1.10.0

Pybind11

https://github.com/pybind/pybind11/