Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add pybind11 example #2324

Merged
merged 8 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/pybind11/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
12 changes: 12 additions & 0 deletions examples/pybind11/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.15...3.29)

project(${SKBUILD_PROJECT_NAME} VERSION ${SKBUILD_PROJECT_VERSION} LANGUAGES CXX)

set(PYBIND11_NEWPYTHON ON)
find_package(pybind11 CONFIG REQUIRED)

file(GLOB SOURCES "src/*.cpp")

pybind11_add_module(${PROJECT_NAME} ${SOURCES})

install(TARGETS ${PROJECT_NAME} DESTINATION .)
60 changes: 60 additions & 0 deletions examples/pybind11/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# pybind11

This example demonstrates how to compile pybind11 modules using Pixi and `scikit-build-core`.

## Overview

[pybind11](https://github.com/pybind/pybind11) is a lightweight header-only library that exposes C++ types in Python and vice versa, mainly to create Python bindings of existing C++ code. This project showcases an efficient way to build pybind11 modules using modern tools.

## Prerequisites

- [Pixi](https://github.com/prefix-dev/pixi) installed on your system
- Basic knowledge of C++ and Python

## Usage

To build the project, run the following command in your terminal:

```sh
pixi run build
```

This command will:

1. Compile the C++ code
2. Generate Python bindings
3. Create a `dist` folder containing:
- A source distribution (`.tar.gz`) file
- A wheel (`.whl`) file

The source distribution includes the wheel file along with other project-related files, making it ready for publication to a Python package repository like PyPI.

## Installing the wheel

After building the project, you can install the generated wheel file for local testing. Follow these steps:

1. Navigate to the `dist` folder:
```sh
cd dist
```

2. Install the wheel file using pip:
```sh
pip install your_package_name-version-py3-none-any.whl
```
Replace `your_package_name-version-py3-none-any.whl` with the actual name of your wheel file.

3. You can now import and use your module in Python:
```python
import your_module_name
# Use your module here
```

## Next Steps

- Explore the generated files in the `dist` folder
- Test the compiled module in a Python environment
- Consider publishing your package to PyPI or a private repository
- Create a different environment per Python version to generate multiple `.whl` files.

For more information on pybind11, visit the [official documentation](https://pybind11.readthedocs.io/).
Loading
Loading