Cython C++ Guide: Complete Tutorial with Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Cython for C++: The Complete Guide with Examples

Welcome to PythonLib! Today we're diving into the fascinating world of Cython — a powerful tool that combines Python's flexibility with the raw performance of C and C++. If you've ever dreamed of writing high-performance Python extensions in C++ without leaving Python's familiar syntax, this article is for you.

1. What is Cython and Why Do You Need It?

Cython is a compiled programming language that is a superset of Python. It lets you write code that gets translated into efficient C/C++ code, then compiled into dynamic libraries (Python extensions). The main goal of Cython is to speed up Python programs and provide direct access to C/C++ libraries.

Why bother? Python, being an interpreted language, often falls short in performance compared to compiled languages. Cython solves this by letting you:

  • Speed up critical code sections by 10-100x through static typing and compilation.
  • Integrate existing C/C++ libraries (like OpenCV, NumPy, TensorFlow) without writing pure C wrappers.
  • Build high-performance Python modules for scientific computing, data processing, machine learning, and game development.
  • Keep Python's familiar syntax with minimal changes to achieve C-like speed.

Cython is especially popular in the scientific Python ecosystem (SciPy, scikit-learn, pandas) where performance is critical. It's also used in web frameworks (like uWSGI) and game engines (Pygame).

2. Installing Cython

Installing Cython is straightforward using pip, Python's standard package manager. For C++ support, you'll also need a C++ compiler (g++, clang++, or MSVC).

# Install Cython
pip install cython

# Also install setuptools for development
pip install setuptools

# Verify the installation
python -c "import cython; print(cython.__version__)"

Note: If you're using Anaconda, Cython comes pre-installed. To compile C++ code, make sure you have a compiler:

3. Quick Start: A Minimal Working Example

Let's create a simple Cython extension that adds two numbers. This demonstrates the basic workflow: writing a .pyx file, creating a setup.py, and compiling.

Step 1: Create hello.pyx

# hello.pyx
def add(int a, int b):
    return a + b

Step 2: Create setup.py

# setup.py
from setuptools import setup
from Cython.Build import cythonize

setup(
    ext_modules=cythonize("hello.pyx", language="c++"),  # language="c++" for C++
)

Step 3: Compile the Extension

python setup.py build_ext --inplace

After compilation, you'll see a hello.cpython-3XX-*.so file (or .pyd on Windows).

Step 4: Use the Module in Python

# test.py
import hello
print(hello.add(5, 3))  # Output: 8

That's it! You've just created your first Cython extension in C++. Behind the scenes, Cython generated C++ code, compiled it, and created a dynamic library.

Recommendations