Skip to main content

autopxd2

Automatically generate Cython .pxd declaration files from C/C++ header files.

PyPI version Python versions Test Documentation License

Overview

autopxd2 parses C and C++ header files and generates Cython .pxd declaration files, enabling you to call native C/C++ libraries from Cython without manually writing boilerplate declarations.

Key features:

  • Generates complete .pxd declaration files from C/C++ headers
  • Powered by headerkit, with a choice of parser backends: LLVM libclang (supporting LLVM 18–23) or tree-sitter (no system LLVM required)
  • Full C++ support: classes, methods, constructors, multiple inheritance, templates, and namespaces
  • Automatic system include path detection
  • Auto-generates cimport statements for standard library types and bundled stubs
  • Cross-platform support (Linux, macOS, Windows)

Supported Features

C Features

Feature Supported
Structs and unions ✓
Enums (with expressions) ✓
Typedefs ✓
Function declarations ✓
Function pointers ✓
Arrays (fixed and flexible) ✓
Pointers and const qualifiers ✓
Forward declarations ✓
Anonymous structs/unions ✓
Bit fields ✓
#define macros (int, float, string) ✓
Circular type dependencies ✓

C++ Features

Feature Supported
Classes and structs (cppclass) ✓
Constructors ✓
Destructors not emitted — Cython cppclass blocks do not declare them
Methods (const, @staticmethod) ✓
Multiple inheritance ✓
Namespaces (including nested) ✓
Generic function and method templates (T max[T](T a, T b)) ✓
Class templates with type parameters ✓
Template specializations ✓
Operator overloading (renamed for Cython) ✓
References and const references ✓

Installation

pip install autopxd2

autopxd2 uses headerkit, which vendors official LLVM clang Python bindings for LLVM 18–23 directly. No separate clang or clang2 Python package is needed.

You only need the system libclang shared library (libclang.so, libclang.dylib, or libclang.dll).

If libclang is not already installed on your system, install it via your system package manager:

  • macOS: brew install llvm
  • Ubuntu/Debian: sudo apt-get install libclang-dev
  • Fedora/RHEL: sudo dnf install clang-devel
  • Windows: winget install LLVM.LLVM

Or use HeaderKit's built-in installer:

python -m headerkit.install_libclang

See the installation docs for more details.

Alternative: Tree-sitter Backend (No LLVM Dependency)

To parse C and C++ headers without requiring system libclang, install the treesitter extra:

pip install "autopxd2[treesitter]"

Then specify --backend tree-sitter:

autopxd --backend tree-sitter myheader.h myheader.pxd

The tree-sitter backend parses the given file only. Compared to libclang it does not:

  • follow #include directives, so declarations from included headers are absent
  • extract #define macros as constants
  • detect system include paths
  • accept --std or --clang-arg (both are rejected with an error)

Quick Start

# Generate a .pxd file from a C header
autopxd myheader.h myheader.pxd

# Parse a C++ header
autopxd --cpp myclass.hpp myclass.pxd

# Add project-specific include directories (system includes are auto-detected)
autopxd -I ./include myheader.h myheader.pxd

# Write to stdout (omit output file or use '-')
autopxd myheader.h > myheader.pxd

# Read from stdin
cat myheader.h | autopxd -

Usage

autopxd [OPTIONS] [INFILE] [OUTFILE]

Options:
  -v, --version                  Print version and exit.
  -b, --backend TEXT             Parser backend (default: auto, using libclang).
  --list-backends                List available backends and exit.
  --json                         JSON output (with --list-backends).
  -q, --quiet                    Suppress warnings.
  --debug / --no-debug           Print debug info to stderr.
  -I, --include-dir <dir>        Add include search path.
  -D, --define <macro>           Define preprocessor macro.
  --allowlist <pattern>          Only emit declarations from files matching pattern.
  --denylist <pattern>           Never emit declarations from files matching pattern.
                                 Wins over --allowlist.
  -w, --whitelist <pattern>      Deprecated alias for --allowlist.
  -x, --cpp                      Parse as C++.
  --std <std>                    Language standard (e.g., c11, c++17).
  --clang-arg <arg>              Pass argument to clang.
  --no-default-includes          Disable system include auto-detection.
  -P, --project-prefix <path>    Treat path as project (not system) for umbrella headers.
  --no-recursive                 Disable recursive parsing of umbrella headers.
  --max-depth <n>                Max recursion depth for umbrella headers (default: 10).
  -h, --help                     Show this message and exit.

By default, declarations from every non-system included header are emitted. --no-recursive confines the output to the input header's own declarations. --allowlist narrows the emitted set in either mode, and --denylist subtracts from it; a file matching both lists is excluded, because deny wins over allow.

Automatic Imports

autopxd2 automatically generates cimport statements for commonly used types when using the libclang backend.

Cython Standard Library Types

When headers use types from C standard library headers, autopxd generates appropriate Cython cimports:

// input.h
#include <stdio.h>
#include <stdint.h>

uint32_t process(FILE *f);
# Generated output
from libc.stdint cimport uint32_t
from libc.stdio cimport FILE

cdef extern from "input.h":
    uint32_t process(FILE* f)

Bundled Stub Types

For types not in Cython's standard library, autopxd provides bundled stub declarations:

// input.h
#include <stdarg.h>

void log_message(const char *fmt, va_list args);
# Generated output
from headerkit.stubs.stdarg cimport va_list

cdef extern from "input.h":
    void log_message(const char* fmt, va_list args)

C++ STL Types

C++ standard library types are automatically imported from libcpp:

// input.hpp
#include <vector>
#include <string>

std::vector<std::string> get_items();
# Generated output
from libcpp.string cimport string
from libcpp.vector cimport vector

cdef extern from "input.hpp":
    vector[string] get_items()

Documentation

Full documentation is available at elijahr.github.io/python-autopxd2.

Docker

A Docker image with libclang pre-installed is available:

docker run --rm -v $(pwd):/work -w /work ghcr.io/elijahr/python-autopxd2 autopxd myheader.h

See Docker Usage for more examples.

Contributing

Contributions are welcome. See CONTRIBUTING.md for development setup and guidelines.

License

MIT License. See LICENSE for details.

Acknowledgments

This project is a fork of python-autopxd by Thiago de Arruda.

Metadata

Release files for autopxd2 4.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for autopxd2 4.0.0
File Size Uploaded
autopxd2-4.0.0.tar.gz 95.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for autopxd2 4.0.0
File Interpreter ABI Platform
autopxd2-4.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 108.6 kB

Release files / autopxd2-4.0.0.tar.gz

Download URL autopxd2-4.0.0.tar.gz
Size 95.2 kB
Tags Source
SHA-256 checksum
How to use checksums
09d82a25ef86db073dde8b1970df910e5630293d8c2c7ff6025a5b0469d12742
BLAKE2b-256 checksum
How to use checksums
8519e58aef10cf8024de3b16d547caf781c126bb8eaa1c47a3f5211d4d7cd0bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

Release files / autopxd2-4.0.0-py3-none-any.whl

Download URL autopxd2-4.0.0-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
52983aca3f57f416a0652a69d26a0e2a371a00549b163c59eecea562087a2409
BLAKE2b-256 checksum
How to use checksums
d27a08750d780e18ac0c59fc04aba248790e4aecf8da3a4c6514981333a0dace
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

Release history Release notifications | RSS feed

4.0.1

2 release files

This release

4.0.0 This release

2 release files

3.2.3

2 release files

3.2.2

2 release files

3.1.1

2 release files

3.0.0

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

1 release file

2.2.3

1 release file

2.2.2

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

1.1.0

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page