Why Build C++ with Pixi?
Pixi’s C++ build support enables:- Native performance with cross-platform builds
- Python bindings using nanobind or pybind11
- Mixed language projects combining C++ and Python
- Conda ecosystem for C++ dependencies like SDL2, Boost, etc.
Creating a C++ Package with Python Bindings
#include <nanobind/nanobind.h>
int add(int a, int b) { return a + b; } // (1)!
NB_MODULE(cpp_math, m)
{
m.def("add", &add); // (2)!
}
cmake_minimum_required(VERSION 3.20...3.27)
project(cpp_math)
find_package(Python 3.8 COMPONENTS Interpreter Development.Module REQUIRED) # (1)!
execute_process(
COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE nanobind_ROOT
) # (2)!
execute_process(
COMMAND ${Python_EXECUTABLE} -c "import sysconfig; print(sysconfig.get_path('purelib'))"
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES
OUTPUT_STRIP_TRAILING_WHITESPACE
) # (3)!
find_package(nanobind CONFIG REQUIRED) # (4)!
nanobind_add_module(${PROJECT_NAME} src/math.cpp) # (5)!
install( # (6)!
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION ${PYTHON_SITE_PACKAGES}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${BINDIR}
)
[workspace]
channels = ["https://prefix.dev/conda-forge"]
platforms = ["osx-arm64", "linux-64", "osx-64", "win-64"]
preview = ["pixi-build"] # (1)!
[dependencies] # (2)!
cpp_math = { path = "." }
python = "*"
[tasks]
start = "python -c 'import cpp_math as b; print(b.add(1, 2))'" # (3)!
[package] # (4)!
name = "cpp_math"
version = "0.1.0"
[package.build] # (5)!
backend = { name = "pixi-build-cmake", version = "0.3.*" }
[package.build.config]
extra-args = ["-DCMAKE_BUILD_TYPE=Release"] # (6)!
[package.host-dependencies]
cmake = "3.20.*" # (7)!
nanobind = "2.4.*" # (8)!
python = "3.12.*" # (9)!
When using
pixi-build-cmake, you don’t need to specify compilers - the backend installs CMake, Ninja, and C++ compilers automatically.Building C++ Applications
SDL2 Example
Here’s a complete example building an SDL2 application:pixi.toml
CMakeLists.txt
Advanced Configuration
Custom CMake Arguments
Pass additional CMake configuration:Specifying Compilers
The
pixi-build-cmake backend automatically provides compilers. However, for special cases:Cross-Compilation Example
For cross-compilation from macOS ARM to Linux x86_64:Using Git Sources
Build from a git repository:pixi.toml
Dependency Types for C++
Build Dependencies
Build Dependencies
Tools that run on your build machine:
For
pixi-build-cmake, these are provided automatically.Host Dependencies
Host Dependencies
Libraries and headers for the target platform:These must match your target platform, not build platform.
Run Dependencies
Run Dependencies
Libraries needed at runtime:Many conda packages have
run-exports that automatically add run dependencies based on host dependencies.Real-World Example: Mixed Project
Directory Structure
Root pixi.toml
pixi.toml
Using C++ from Python
src/python_rich/__init__.py
Next Steps
Workspaces
Combine C++ and Python packages
Dependency Types
Understand build, host, and run dependencies
Package Sources
Build from git or custom paths
Build Variants
Build for multiple Python versions
Troubleshooting
CMake can't find dependencies
CMake can't find dependencies
Ensure dependencies are in The conda environment sets CMAKE_PREFIX_PATH automatically.
host-dependencies:Python bindings not found
Python bindings not found
Check the install path in CMakeLists.txt uses
PYTHON_SITE_PACKAGES:Cross-compilation issues
Cross-compilation issues
Verify platform settings:
- Build platform: Your machine’s architecture
- Host/target platform: Where the code will run