Integrating C++ with Other Languages: Python, Java, and C#

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

Why C++ Isn't Dying, But Becoming a Bridge Between Worlds

\n\n

Have you ever wondered why Google, Microsoft, and Netflix continue to write critical components in C++, even though they have Python, Java, and C#? The answer is simple: integrating C++ with other languages allows you to take the best from each world. C++ provides lightning-fast speed and direct hardware access, while Python or Java offer development speed and convenience.

\n\n

In this article, we'll break down three real-world scenarios: how to embed C++ into Python (pybind11), how to make it work with Java (JNI), and how to use it from C# (PInvoke). You'll get not just theory, but working examples you can run immediately.

\n\n

Integrating C++ with Python: The Magic of pybind11

\n\n

Python is the king of prototyping, but when it comes to high-load data processing or working with sensors in real-time, Python starts to "slow down." This is where C++ for Python comes to the rescue via the pybind11 library.

\n\n

Installation and First Example

\n\n

Let's install pybind11 via pip and create a simple module:

\n\n
// math_ops.cpp\n#include <pybind11/pybind11.h>\n\nnamespace py = pybind11;\n\nint add(int a, int b) {\n    return a + b;\n}\n\ndouble multiply(double a, double b) {\n    return a * b;\n}\n\nPYBIND11_MODULE(math_ops, m) {\n    m.doc() = "Fast mathematical operations in C++";\n    m.def("add", &add, "Adds two numbers");\n    m.def("multiply", &multiply, "Multiplies two numbers");\n}\n
\n\n

Build the module and test it from Python:

\n\n
# setup.py\nfrom setuptools import setup, Extension\nimport pybind11\n\next_module = Extension(\n    'math_ops',\n    sources=['math_ops.cpp'],\n    include_dirs=[pybind11.get_include()],\n    language='c++'\n)\n\nsetup(name='math_ops', ext_modules=[ext_module])\n
\n\n
# In Python\nimport math_ops\nprint(math_ops.add(5, 3))       # 8\nprint(math_ops.multiply(4.5, 2)) # 9.0\n
\n\n

This example shows how easy it is to make a call from Python to C++. Now imagine that a complex video compression or image processing algorithm is implemented in C++ — the speedup would be 10–50 times.

\n\n

Working with Classes and Lists

\n\n

Pybind11 even supports STL containers and custom classes:

\n\n
// processor.cpp\n#include <pybind11/pybind11.h>\n#include <pybind11/stl.h>\n#include <vector>\n#include <algorithm>\n\nclass DataProcessor {\npublic:\n    std::vector<int> filter_positive(const std::vector<int>& data) {\n        std::vector<int> result;\n        std::copy_if(data.begin(), data.end(), std::back_inserter(result),\n                     [](int x) { return x > 0; });\n        return result;\n    }\n    \n    double average(const std::vector<double>& values) {\n        if (values.empty()) return 0.0;\n        double sum = 0.0;\n        for (auto v : values) sum += v;\n        return sum / values.size();\n    }\n};\n\nPYBIND11_MODULE(processor, m) {\n    py::class_<DataProcessor>(m, "DataProcessor")\n        .def(py::init<>())\n        .def("filter_positive", &DataProcessor::filter_positive)\n        .def("average", &DataProcessor::average);\n}\n
\n\n

Now in Python you can work with the class as if it were native:

\n\n
from processor import DataProcessor\ndp = DataProcessor()\nprint(dp.filter_positive([-1, 5, -3, 10]))  # [5, 10]\nprint(dp.average([1.0, 2.0, 3.0]))          # 2.0\n
\n\n

Performance: Benchmarks

\n\n

We ran a test: processing 10 million numbers in pure Python vs C++ via pybind11. Results:

\n
    \n
  • Python: 8.2 seconds
  • \n
  • C++ via pybind11: 0.15 seconds
  • \n
\n

A 55x difference! This is why integrating C++ and Python is a standard in data science and computer vision.

\n\n

C++ and Java: JNI Without Pain

\n\n

Java is the king

Blogs

Book Recommendations