SlideShare a Scribd company logo
www.luxoft.com
Kostiantyn Grygoriev
Technical Lead
Wrapping C/C++ for Python
www.luxoft.com
Growth of Major Programming Languages
Based on Stack Overflow question views in World Bank high-income countries
2
www.luxoft.com
Top Languages Over Time
Based on GitHub statistics by contributors in public and private repositories, organizations of all sizes, and every region of
the world.
3
www.luxoft.com 4
www.luxoft.com
Tools to be Overviewed:
• Ctypes
• SWIG
• Pybind11
• Boost.Python
www.luxoft.com
Ctypes
6
Ctypes is a foreign functions library for
Python that provides C-compatible data
types. Also, you can use ctypes with
libraries written in any language that can
export a C compatible API - e.g. Fortran,
Rust.
The pros and cons:
• ctypes is already included with your Python
installation
• you don’t need to recompile a C/C++ library
to use it from Python
• you need to manually wrap the data and
functions from the foreign library in Python
code
• not good C++ support
www.luxoft.com
How to use Ctypes with C
7
• Compile C/C++ code into a shared
library:
gcc -o example.so -shared -fPIC
example.c
• Tell the system where to find the
shared library:
export LD_LIBRARY_PATH=.
from ctypes import cdll
my_lib = cdll.LoadLibrary(“example.so")
foo = my_lib.fuct
>> print foo(5)
120
• from ctypes import cdll
my_lib = cdll.LoadLibrary(“example.so")
foo = my_lib.getGreetingString
>> print foo("world")
136040696
• from ctypes import c_char_p
foo.restype = c_char_p
And now it will work:
>> print foo(“world”)
hello, world
Build Process Python Code Example 1 Python Code Example 2
www.luxoft.com
How to use Ctypes with C++
8
• char * getGreetingStringWrapper(char *val) {
// char * to std::string
std::string str = getGreetingString(std::string(val));
// std::string to char *
char *ret = new char[str.length() + 1];
strcpy(ret, str.c_str());
return ret;
}
• #ifdef __cplusplus
extern "C" {
#endif
char *getGreetingStringWrapper (char *val);
#ifdef __cplusplus
}
#endif
• nm –D example.so
• 0000000000000bba T _Z4facti
0000000000000cad T
_Z9printLineNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
0000000000000bf2 T _Z9printLinev
U _ZNSaIcEC1Ev
U _ZNSaIcED1Ev
U_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC
1EPKcRKS3
U_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED
U _ZNSt8ios_base4InitD1Ev
U _ZSt4cout
U
_ZStlsIcSt11char_traitsIcESaIcEERSt13basic_ostreamIT_T0_ES7_RKN
St7__cxx1112basic_stringIS4_S5_T1_EE
C/C++ Workaround
www.luxoft.com
SWIG (Simple Wrapper
Interface Generator)
9
SWIG is a software development tool
that connects programs written in C
and C++ with a variety of high-level
programming languages.
The pros and cons:
• Capable of wrapping C/C++ in a large
variety of languages (over 20)
www.luxoft.com
C++ features in SWIG
10
SWIG currently supports most C++
features including the following:
• Classes
• Virtual and static functions
• Public inheritance (including multiple
inheritance)
• Function and method overloading
• Operator overloading for standard operators
• References and smart pointers
• Templates
• Pointers to members
• Default parameters
• Overloaded versions of certain operators
(new, delete, etc.)
• Nested classes
www.luxoft.com
How to Use SWIG
11
SWIG module file : api.i
%module example
%include <std_string.i>
%{
#define SWIG_FILE_WITH_INIT
#include “api.h”
%}
int fact(int n);
std::string getLine();
void printLine(std::string line);
• Build a Python module:
swig -c++ -python api.i
• Compile C/C++ code:
gcc -fPIC -c api.c
gcc -fPIC -c api_wrapper.c
-I/usr/include/python2.7
gcc -shared api.o api_wrapper.o –
o _api.so
import example
print(example.fact(5))
example.printLine(“linen”)
print(example.getLine())
C/C++ Additional Code Build Process Python Code
www.luxoft.com
Pybind11
12
Pybind11 is a lightweight header-only
library that exposes C++ types in Python
and vice versa, mainly to create Python
bindings of existing C++ code.
The pros and cons:
• Header-only
• Uses C++11 move constructors and move
assignment operators whenever possible to
efficiently transfer custom data types
• Can automatically vectorize functions
• Function signatures are precomputed at compile
time (using constexpr), leading to smaller binaries
• C++ types can be pickled and unpickled similar to
regular Python objects
www.luxoft.com
C++ features in Pybind11
13
The following core C++ features can be
mapped to Python
• Functions accepting and returning custom
data structures per value, reference, or
pointer
• Instance methods and static methods
• Overloaded functions
• Arbitrary exception types
• Enumerations
• Callbacks
• Custom operators
• Single and multiple inheritance
• STL data structures and smart pointers
• C++ classes with virtual (and pure virtual)
methods can be extended in Python
www.luxoft.com
How to use Pybind11
14
• #include <pybind11/pybind11.h>
namespace py = pybind11;
PYBIND11_MODULE(my_client, m) {
m.doc() = “My plugin for communication with C++";
m.def(“fact", &API::fact);
m.def(“factorial", &API::fact,"This function calculates
factorial",py::arg(“n"));
m.def(“print", py::overload_cast<std::string&>(&API::printLine));
m.def(“print", py::overload_cast<int>(&API::printLine));
}
• $ g++ -shared -std=c++11
-fPIC `python3 -m pybind11 --includes`
example.cpp -o example`python3-config --extension-
suffix`
C++ Additional Code Build Process
www.luxoft.com
Boost.Python
15
The Boost Python Library is a
framework for interfacing Python and
C++. It allows you to quickly and
seamlessly expose C++ classes
functions and objects to Python, and
vice-versa, using no special tools - just
your C++ compiler.
www.luxoft.com
Boost.Python vs Pybind11
16
• #include <pybind11/pybind11.h>
namespace py = pybind11;
PYBIND11_MODULE(my_client, m) {
m.def(“fact", &API::fact);
}
• #include <boost/python.hpp>
namespace py = boost::python;
BOOST_PYTHON_MODULE(my_client) {
py.def(" fact ", &API::fact);
}
Pybind11 Boost.Python
www.luxoft.com
Boost dependencies
17
1. bind
2. config
3. conversion
4. core
5. detail
6. foreach
7. function
8. iterator
9. lexical_cast
10. mpl
11. numeric~conversion
12. preprocessor
13. smart_ptr
14. static_assert
15. tuple
16. type_traits
17. utility
Primary dependencies for python:
www.luxoft.com
Summary
18
• Ctypes. You need to use C code, and very few C++
functions.
• SWIG. If you going to provide API for at least few
languages.
• Pybind11. For code on C++11 and later standards. You
need to call C++ code from Python and vice-versa.
• Boost.Python. In case you have to support old compilers
or you’ve already had the whole boost at your project :)
When to use?
www.luxoft.com
Links to sources
1. Stack Overflow Trends
2. Octoverse GitHub overview
3. C/C++ из Python (ctypes)
4. SWIG documentation (Python)
5. SWIG documentation (C++)
6. Pybind11
7. Boostdep
8. Boost.Python
9. D-Bus documentation
www.luxoft.com
Thank you!

More Related Content

PDF
Overview of C++20 and a deeper look into modules
PDF
[HES2013] Frida IRE – a tool for scriptable dynamic instrumentation in userla...
PDF
[C++ korea] effective modern c++ study item 4 - 6 신촌
PDF
interface
PPTX
Александр Куцан: "Static Code Analysis in C++"
PDF
C multiple choice questions and answers pdf
PDF
C言語静的解析ツールと Ruby 1.9 trunk
PDF
Prometheus as exposition format for eBPF programs running on Kubernetes
Overview of C++20 and a deeper look into modules
[HES2013] Frida IRE – a tool for scriptable dynamic instrumentation in userla...
[C++ korea] effective modern c++ study item 4 - 6 신촌
interface
Александр Куцан: "Static Code Analysis in C++"
C multiple choice questions and answers pdf
C言語静的解析ツールと Ruby 1.9 trunk
Prometheus as exposition format for eBPF programs running on Kubernetes

Similar to Kostiantyn Grygoriev "Wrapping C++ for Python" (20)

PDF
Apache Flink Adoption at Shopify
DOC
Amit Bhandari
PDF
Reverse_Engineering_of_binary_File_Formats.pdf
PPTX
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
PPTX
Lecture 1_ Introduction to Programming Fundamentals with C++.pptx
PDF
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
PDF
Introduction to interactive data visualisation using R Shiny
PDF
Integrating Existing C++ Libraries into PySpark with Esther Kundin
KEY
PyCon AU 2012 - Debugging Live Python Web Applications
DOCX
C tutorials
PDF
License Plate Recognition System using Python and OpenCV
PDF
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
PDF
Python Linters at Scale.pdf
PDF
Software Engineering
PDF
More about PHP
PDF
Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...
PDF
Larson and toubro
PPTX
MS Day EPITA 2010: Visual Studio 2010 et Framework .NET 4.0
PDF
Kyrylo Cherneha "C++ & Python Interaction in Automotive Industry"
PPTX
Python for IoT CoE.pptx KDOJWIHJNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
Apache Flink Adoption at Shopify
Amit Bhandari
Reverse_Engineering_of_binary_File_Formats.pdf
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Lecture 1_ Introduction to Programming Fundamentals with C++.pptx
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
Introduction to interactive data visualisation using R Shiny
Integrating Existing C++ Libraries into PySpark with Esther Kundin
PyCon AU 2012 - Debugging Live Python Web Applications
C tutorials
License Plate Recognition System using Python and OpenCV
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
Python Linters at Scale.pdf
Software Engineering
More about PHP
Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...
Larson and toubro
MS Day EPITA 2010: Visual Studio 2010 et Framework .NET 4.0
Kyrylo Cherneha "C++ & Python Interaction in Automotive Industry"
Python for IoT CoE.pptx KDOJWIHJNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
Ad

More from LogeekNightUkraine (20)

PPTX
Face recognition with c++
PPTX
C++20 features
PPTX
Autonomous driving on your developer pc. technologies, approaches, future
PDF
Orkhan Gasimov "High Performance System Design"
PPTX
Vitalii Korzh "Managed Workflows or How to Master Data"
PDF
Yevhen Tatarynov "From POC to High-Performance .NET applications"
PDF
Oleksii Kuchuk "Reading gauge values with open cv imgproc"
PDF
Oleksandr Kutsan "Using katai struct to describe the process of working with ...
PDF
Pavlo Zhdanov "Mastering solid and base principles for software design"
PDF
Serhii Zemlianyi "Error Retries with Exponential Backoff Using RabbitMQ"
PDF
Iurii Antykhovych "Java and performance tools and toys"
PDF
Eugene Bova "Dapr (Distributed Application Runtime) in a Microservices Archit...
PPTX
Aleksandr Kutsan "Managing Dependencies in C++"
PDF
Yevhen Tatarynov "My .NET Application Allocates too Much Memory. What Can I Do?"
PDF
Alexandr Golyak, Nikolay Chertkov "Automotive Testing vs Test Automatio"
PPTX
Michal Kordas "Docker: Good, Bad or Both"
PPTX
Kolomiyets Dmytro "Dealing with Multiple Caches, When Developing Microservices"
PPTX
Shestakov Illia "The Sandbox Theory"
PPTX
Dmytro Kochergin “Autotest with CYPRESS”
PPTX
Ivan Dryzhyruk “Ducks Don’t Like Bugs”
Face recognition with c++
C++20 features
Autonomous driving on your developer pc. technologies, approaches, future
Orkhan Gasimov "High Performance System Design"
Vitalii Korzh "Managed Workflows or How to Master Data"
Yevhen Tatarynov "From POC to High-Performance .NET applications"
Oleksii Kuchuk "Reading gauge values with open cv imgproc"
Oleksandr Kutsan "Using katai struct to describe the process of working with ...
Pavlo Zhdanov "Mastering solid and base principles for software design"
Serhii Zemlianyi "Error Retries with Exponential Backoff Using RabbitMQ"
Iurii Antykhovych "Java and performance tools and toys"
Eugene Bova "Dapr (Distributed Application Runtime) in a Microservices Archit...
Aleksandr Kutsan "Managing Dependencies in C++"
Yevhen Tatarynov "My .NET Application Allocates too Much Memory. What Can I Do?"
Alexandr Golyak, Nikolay Chertkov "Automotive Testing vs Test Automatio"
Michal Kordas "Docker: Good, Bad or Both"
Kolomiyets Dmytro "Dealing with Multiple Caches, When Developing Microservices"
Shestakov Illia "The Sandbox Theory"
Dmytro Kochergin “Autotest with CYPRESS”
Ivan Dryzhyruk “Ducks Don’t Like Bugs”
Ad

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPT
Teaching material agriculture food technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Empathic Computing: Creating Shared Understanding
PDF
cuic standard and advanced reporting.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Modernizing your data center with Dell and AMD
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Approach and Philosophy of On baking technology
Per capita expenditure prediction using model stacking based on satellite ima...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Dropbox Q2 2025 Financial Results & Investor Presentation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Teaching material agriculture food technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Empathic Computing: Creating Shared Understanding
cuic standard and advanced reporting.pdf
A Presentation on Artificial Intelligence
Building Integrated photovoltaic BIPV_UPV.pdf
Understanding_Digital_Forensics_Presentation.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Modernizing your data center with Dell and AMD
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

Kostiantyn Grygoriev "Wrapping C++ for Python"

  • 2. www.luxoft.com Growth of Major Programming Languages Based on Stack Overflow question views in World Bank high-income countries 2
  • 3. www.luxoft.com Top Languages Over Time Based on GitHub statistics by contributors in public and private repositories, organizations of all sizes, and every region of the world. 3
  • 5. www.luxoft.com Tools to be Overviewed: • Ctypes • SWIG • Pybind11 • Boost.Python
  • 6. www.luxoft.com Ctypes 6 Ctypes is a foreign functions library for Python that provides C-compatible data types. Also, you can use ctypes with libraries written in any language that can export a C compatible API - e.g. Fortran, Rust. The pros and cons: • ctypes is already included with your Python installation • you don’t need to recompile a C/C++ library to use it from Python • you need to manually wrap the data and functions from the foreign library in Python code • not good C++ support
  • 7. www.luxoft.com How to use Ctypes with C 7 • Compile C/C++ code into a shared library: gcc -o example.so -shared -fPIC example.c • Tell the system where to find the shared library: export LD_LIBRARY_PATH=. from ctypes import cdll my_lib = cdll.LoadLibrary(“example.so") foo = my_lib.fuct >> print foo(5) 120 • from ctypes import cdll my_lib = cdll.LoadLibrary(“example.so") foo = my_lib.getGreetingString >> print foo("world") 136040696 • from ctypes import c_char_p foo.restype = c_char_p And now it will work: >> print foo(“world”) hello, world Build Process Python Code Example 1 Python Code Example 2
  • 8. www.luxoft.com How to use Ctypes with C++ 8 • char * getGreetingStringWrapper(char *val) { // char * to std::string std::string str = getGreetingString(std::string(val)); // std::string to char * char *ret = new char[str.length() + 1]; strcpy(ret, str.c_str()); return ret; } • #ifdef __cplusplus extern "C" { #endif char *getGreetingStringWrapper (char *val); #ifdef __cplusplus } #endif • nm –D example.so • 0000000000000bba T _Z4facti 0000000000000cad T _Z9printLineNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE 0000000000000bf2 T _Z9printLinev U _ZNSaIcEC1Ev U _ZNSaIcED1Ev U_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC 1EPKcRKS3 U_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED U _ZNSt8ios_base4InitD1Ev U _ZSt4cout U _ZStlsIcSt11char_traitsIcESaIcEERSt13basic_ostreamIT_T0_ES7_RKN St7__cxx1112basic_stringIS4_S5_T1_EE C/C++ Workaround
  • 9. www.luxoft.com SWIG (Simple Wrapper Interface Generator) 9 SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. The pros and cons: • Capable of wrapping C/C++ in a large variety of languages (over 20)
  • 10. www.luxoft.com C++ features in SWIG 10 SWIG currently supports most C++ features including the following: • Classes • Virtual and static functions • Public inheritance (including multiple inheritance) • Function and method overloading • Operator overloading for standard operators • References and smart pointers • Templates • Pointers to members • Default parameters • Overloaded versions of certain operators (new, delete, etc.) • Nested classes
  • 11. www.luxoft.com How to Use SWIG 11 SWIG module file : api.i %module example %include <std_string.i> %{ #define SWIG_FILE_WITH_INIT #include “api.h” %} int fact(int n); std::string getLine(); void printLine(std::string line); • Build a Python module: swig -c++ -python api.i • Compile C/C++ code: gcc -fPIC -c api.c gcc -fPIC -c api_wrapper.c -I/usr/include/python2.7 gcc -shared api.o api_wrapper.o – o _api.so import example print(example.fact(5)) example.printLine(“linen”) print(example.getLine()) C/C++ Additional Code Build Process Python Code
  • 12. www.luxoft.com Pybind11 12 Pybind11 is a lightweight header-only library that exposes C++ types in Python and vice versa, mainly to create Python bindings of existing C++ code. The pros and cons: • Header-only • Uses C++11 move constructors and move assignment operators whenever possible to efficiently transfer custom data types • Can automatically vectorize functions • Function signatures are precomputed at compile time (using constexpr), leading to smaller binaries • C++ types can be pickled and unpickled similar to regular Python objects
  • 13. www.luxoft.com C++ features in Pybind11 13 The following core C++ features can be mapped to Python • Functions accepting and returning custom data structures per value, reference, or pointer • Instance methods and static methods • Overloaded functions • Arbitrary exception types • Enumerations • Callbacks • Custom operators • Single and multiple inheritance • STL data structures and smart pointers • C++ classes with virtual (and pure virtual) methods can be extended in Python
  • 14. www.luxoft.com How to use Pybind11 14 • #include <pybind11/pybind11.h> namespace py = pybind11; PYBIND11_MODULE(my_client, m) { m.doc() = “My plugin for communication with C++"; m.def(“fact", &API::fact); m.def(“factorial", &API::fact,"This function calculates factorial",py::arg(“n")); m.def(“print", py::overload_cast<std::string&>(&API::printLine)); m.def(“print", py::overload_cast<int>(&API::printLine)); } • $ g++ -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension- suffix` C++ Additional Code Build Process
  • 15. www.luxoft.com Boost.Python 15 The Boost Python Library is a framework for interfacing Python and C++. It allows you to quickly and seamlessly expose C++ classes functions and objects to Python, and vice-versa, using no special tools - just your C++ compiler.
  • 16. www.luxoft.com Boost.Python vs Pybind11 16 • #include <pybind11/pybind11.h> namespace py = pybind11; PYBIND11_MODULE(my_client, m) { m.def(“fact", &API::fact); } • #include <boost/python.hpp> namespace py = boost::python; BOOST_PYTHON_MODULE(my_client) { py.def(" fact ", &API::fact); } Pybind11 Boost.Python
  • 17. www.luxoft.com Boost dependencies 17 1. bind 2. config 3. conversion 4. core 5. detail 6. foreach 7. function 8. iterator 9. lexical_cast 10. mpl 11. numeric~conversion 12. preprocessor 13. smart_ptr 14. static_assert 15. tuple 16. type_traits 17. utility Primary dependencies for python:
  • 18. www.luxoft.com Summary 18 • Ctypes. You need to use C code, and very few C++ functions. • SWIG. If you going to provide API for at least few languages. • Pybind11. For code on C++11 and later standards. You need to call C++ code from Python and vice-versa. • Boost.Python. In case you have to support old compilers or you’ve already had the whole boost at your project :) When to use?
  • 19. www.luxoft.com Links to sources 1. Stack Overflow Trends 2. Octoverse GitHub overview 3. C/C++ из Python (ctypes) 4. SWIG documentation (Python) 5. SWIG documentation (C++) 6. Pybind11 7. Boostdep 8. Boost.Python 9. D-Bus documentation