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 header files and generates Cython .pxd files, enabling you to call C libraries from Cython without manually writing declarations.

Key features:

  • Generates complete .pxd files from C/C++ headers
  • Full C++ support with libclang backend
  • Automatic system include path detection
  • Auto-generates cimport statements for standard library types
  • Cross-platform support (Linux, macOS, Windows)

Supported Features

C Features

Feature libclang pycparser (legacy)
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 (libclang only)

Feature Supported
Classes with fields and methods
Namespaces (including nested)
Templates with type parameters
Template specializations
Operator overloading (renamed for Cython)
References and const references
Inheritance declarations
Static methods and fields

Installation

pip install autopxd2

Recommended: Install the libclang backend for full C/C++ support. The clang2 package version must match your system's LLVM version:

# One-liner: detect LLVM version and install matching clang2
pip install "clang2==$(llvm-config --version | cut -d. -f1).*"

# Or check version manually first
llvm-config --version  # e.g., 18.1.3 means you need clang2==18.*
pip install "clang2==18.*"

Without clang2, autopxd2 falls back to the legacy pycparser backend (C99 only, no macros, no circular dependency handling). If clang2 is missing, autopxd2 will detect your LLVM version and show the exact install command.

See the installation docs for platform-specific LLVM setup (macOS with Homebrew, Ubuntu/Debian, Windows).

Quick Start

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

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

# Write to stdout (omit output file)
autopxd myheader.h > myheader.pxd

Usage

autopxd [OPTIONS] INFILE [OUTFILE]

Options marked [libclang] require the libclang backend.

Options:
  -v, --version              Print version and exit.
  -b, --backend <name>       Parser backend (default: auto, prefers libclang).
  --list-backends            List available backends and exit.
  -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.
  -w, --whitelist <pattern>  Only emit from files matching pattern.
  -x, --cpp                  [libclang] Parse as C++.
  --std <std>                [libclang] Language standard (e.g., c11, c++17).
  --clang-arg <arg>          [libclang] Pass argument to clang.
  --no-default-includes      [libclang] Disable system include auto-detection.
  -p, --project-prefix       [libclang] Whitelist paths for umbrella headers.
  --no-recursive             [libclang] Disable recursive include parsing.
  --max-depth <n>            [libclang] Max depth for recursive includes.
  -h, --help                 Show this message and exit.

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 autopxd.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()

Note: Auto-import requires the libclang backend for header detection. The pycparser backend does not track included headers.

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.

Release files for autopxd2 3.2.3

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 3.2.3
File Size Uploaded
autopxd2-3.2.3.tar.gz 233.8 kB Details

Built distribution (wheel)

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

Total release size: 963.9 kB

Release files / autopxd2-3.2.3.tar.gz

Download URL autopxd2-3.2.3.tar.gz
Size 233.8 kB
Tags Source
SHA-256 checksum
How to use checksums
65fe3882692e5afa7c95fad272ae060213a2b0b62ee2c732b8da750a7de59d57
BLAKE2b-256 checksum
How to use checksums
2fe099047862ff21b0dbe694d85c12fa826c5fe3179caef4465118b6cd11c231
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 5, 2026.

Transparency log

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

Download URL autopxd2-3.2.3-py3-none-any.whl
Size 730.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
18438d6178191b4eaa22aa114a93fff737681defc6d81afa446590a27429bd2e
BLAKE2b-256 checksum
How to use checksums
473f1a2def0be88bb60e7df68a1716449f6e598571fe0bf24397bce5f509559c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 5, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

3.2.3 This release

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