Skip to main content

Professional C++ Python bindings with type-generic templates, pystubs and native threading

Project description

IncludeCPP

Write C++ code, use it in Python. Auto-generates pybind11 bindings.

pip install IncludeCPP

First Steps

Project Setup

includecpp init

Creates:

  • cpp.proj - project configuration
  • include/ - your C++ source files
  • plugins/ - generated binding definitions

Write C++ Code

Put your code in namespace includecpp:

// include/fast_list.cpp
#include <vector>

namespace includecpp {

class FastList {
public:
    void append(int val) { data.push_back(val); }
    int get(int i) { return data[i]; }
private:
    std::vector<int> data;
};

int add(int a, int b) { return a + b; }

}  // namespace includecpp

The parser only scans code inside namespace includecpp. Everything else is ignored.

Generate Bindings

includecpp plugin fast_list include/fast_list.cpp

This runs a C++ parser that:

  1. Scans your source files for classes, methods, functions
  2. Extracts signatures, return types, parameter names
  3. Generates plugins/fast_list.cp with binding instructions

Build

includecpp rebuild

Compiles your C++ into a Python extension module (.pyd on Windows, .so on Linux/Mac).

Use in Python

from includecpp import fast_list

my_list = fast_list.FastList()
my_list.append(42)
print(fast_list.add(1, 2))  # 3

Alternative syntax:

from includecpp import CppApi

api = CppApi()
fast_list = api.include("fast_list")

How to Start

Minimal Example

# 1. Create project
mkdir myproject && cd myproject
includecpp init

# 2. Write C++ (include/math.cpp)
cat > include/math.cpp << 'EOF'
namespace includecpp {
    int square(int x) { return x * x; }
}
EOF

# 3. Generate plugin
includecpp plugin math include/math.cpp

# 4. Build
includecpp rebuild

# 5. Use
python -c "from includecpp import math; print(math.square(7))"

Development Workflow

For active development, use auto:

includecpp auto math

This regenerates the .cp file from source and rebuilds in one command.

For fastest iteration:

includecpp rebuild --fast

Skips unchanged files. ~0.4s when nothing changed.

How IncludeCPP Works

Architecture

Your C++ Source            Plugin File (.cp)           Python Module
include/math.cpp    --->   plugins/math.cp     --->    math.cpXXX.pyd
     ^                           ^                          ^
     |                           |                          |
  C++ parser              Binding config             pybind11 compiled
  extracts API            (editable)                 extension

The Parser

The C++ parser (parser.cpp) runs as a compiled executable. It:

  1. Tokenizes your C++ source files
  2. Identifies the namespace includecpp block
  3. Extracts:
    • Class names and inheritance
    • Method signatures (name, return type, parameters)
    • Function signatures
    • Template instantiations
    • Const/static qualifiers
  4. Outputs structured binding instructions to .cp files

Plugin Files (.cp)

The .cp format is a declarative binding specification:

SOURCE(math.cpp) math

PUBLIC:

math CLASS(Calculator) {
    CONSTRUCTOR()
    CONSTRUCTOR(int)
    METHOD(add)
    METHOD_CONST(getValue)
}

math FUNC(square)
math TEMPLATE_FUNC(maximum) TYPES(int, float, double)

Key directives:

  • SOURCE(file.cpp) module_name - links source to module
  • CLASS(Name) - expose a class
  • STRUCT(Name) - expose a struct
  • FUNC(name) - expose a free function
  • METHOD(name) - expose a class method
  • METHOD_CONST(name, signature) - for overloaded methods
  • TEMPLATE_FUNC(name) TYPES(...) - instantiate template
  • CONSTRUCTOR(args) - expose constructor
  • FIELD(type, name) - expose member variable
  • DEPENDS(mod1, mod2) - declare module dependencies

Build System

The build manager:

  1. Reads cpp.proj configuration
  2. Parses all .cp files in plugins/
  3. Generates pybind11 binding code
  4. Compiles using CMake with detected compiler (MSVC, GCC, Clang)
  5. Places output in ~/.includecpp/builds/ (not in your project)
  6. Creates a registry so Python can find modules

Caching layers:

  • Object files: Only recompile changed .cpp files
  • Generator cache: Compiler/CMake detection runs once
  • SHA256 hashes: Skip unchanged modules entirely

CLI Reference

Use includecpp <command> --help for details.

Command Description
init Create project structure
plugin <name> <files> Generate .cp from C++ sources
auto <plugin> Regenerate .cp and rebuild
rebuild / build Compile all modules
get <module> Show module API (classes, methods, functions)
install <name> Install community module
update Update IncludeCPP
bug Report an issue
--doc Show documentation

Build Flags

includecpp rebuild                  # Standard build
includecpp rebuild --clean          # Full rebuild, clear caches
includecpp rebuild --fast           # Fast incremental (~0.4s if unchanged)
includecpp rebuild --verbose        # Show compiler output
includecpp rebuild -m crypto        # Build specific module only
includecpp rebuild -j 8             # Use 8 parallel jobs
includecpp rebuild --keep           # Keep generator between builds
includecpp rebuild --no-incremental # Force full recompilation
includecpp rebuild --this           # Build current directory as module

Fast Mode

--fast enables object file caching:

Scenario Time
No changes ~0.4s
Source changed ~5-10s
Full rebuild ~30s

Clear caches with --clean.

Incompatible Flag Combinations

Flags Reason
--fast + --no-incremental Fast mode requires incremental
--fast + --clean Fast uses caches, clean deletes them
--fast + --this Not supported together
--incremental + --no-incremental Contradictory

Advanced Features

Overloaded Methods

Specify the signature to disambiguate:

MODULE CLASS(Circle) {
    METHOD_CONST(intersects, const Circle&)
    METHOD_CONST(intersects, const Rect&)
}

Template Instantiation

MODULE TEMPLATE_FUNC(maximum) TYPES(int, float, double)

MODULE STRUCT(Point) TYPES(int, float) {
    FIELD(T, x)
    FIELD(T, y)
}

Generates maximum_int, maximum_float, maximum_double and Point_int, Point_float.

Module Dependencies

DEPENDS(math_utils, geometry)

Ensures dependent modules build first.

VSCode IntelliSense

Generates .pyi stub files for autocomplete. Enable in cpp.proj:

{
  "CPI-IntelliSense": true
}

Configuration

cpp.proj

{
  "project": "MyProject",
  "include": "/include",
  "plugins": "/plugins",
  "compiler": {
    "standard": "c++17",
    "optimization": "O3"
  }
}

Options:

  • project - project name
  • include - C++ source directory
  • plugins - plugin file directory
  • compiler.standard - C++ standard (c++11, c++14, c++17, c++20)
  • compiler.optimization - optimization level (O0, O1, O2, O3)

Requirements

  • Python 3.8+
  • C++ compiler (g++, clang++, MSVC)
  • pybind11 (installed automatically)
  • CMake (for build generation)

Changelog

v2.9.16

  • Fixed plugin generator detecting initializer list fields as methods (e.g., seed(0))
  • Fixed nested template parsing in method parameters (e.g., std::vector<std::pair<int, float>>)
  • Improved regex patterns for return type detection

v2.9.15

  • Fixed method signature display in get command
  • Fixed install cycle detection for recursive dependencies
  • Fixed upload error reporting
  • Fixed hash comparison for incremental builds
  • Removed deprecated version comments

v2.9.10

  • New auto command: regenerate .cp + build in one step
  • --fast mode: 30s to 0.4s for unchanged builds
  • Object file caching
  • CMake generator caching
  • Incompatible flag validation

MIT License | v2.9.16 | GitHub

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

includecpp-2.9.16.tar.gz (96.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

includecpp-2.9.16-py3-none-any.whl (102.3 kB view details)

Uploaded Python 3

File details

Details for the file includecpp-2.9.16.tar.gz.

File metadata

  • Download URL: includecpp-2.9.16.tar.gz
  • Upload date:
  • Size: 96.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for includecpp-2.9.16.tar.gz
Algorithm Hash digest
SHA256 05be79451cf20d0095f27a7e49b59db0489144450f9fb119415717ee3e846b72
MD5 e38d25505598159d5e1e0150e9eb21d0
BLAKE2b-256 afa665b87776e8f164d5709f6b4aa6ac2430dae22c71f2d6179eabfc8ec552b3

See more details on using hashes here.

File details

Details for the file includecpp-2.9.16-py3-none-any.whl.

File metadata

  • Download URL: includecpp-2.9.16-py3-none-any.whl
  • Upload date:
  • Size: 102.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for includecpp-2.9.16-py3-none-any.whl
Algorithm Hash digest
SHA256 ff12c00614726bd4a23b5b6de09d6547150c67edf499a5386e1ecc92e57b829b
MD5 514e1fa07914088d73612d3e920e35ed
BLAKE2b-256 102e645c4113cd269ebce41c86d1d2b3614a438cac7e7fc04e347e664390b0c0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page