SlideShare a Scribd company logo
1 of 20
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

Similar to Kostiantyn Grygoriev "Wrapping C++ for Python"

[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
Sang Don Kim
 
MS Day EPITA 2010: Visual Studio 2010 et Framework .NET 4.0
MS Day EPITA 2010: Visual Studio 2010 et Framework .NET 4.0MS Day EPITA 2010: Visual Studio 2010 et Framework .NET 4.0
MS Day EPITA 2010: Visual Studio 2010 et Framework .NET 4.0
Thomas Conté
 
Programming for non tech entrepreneurs
Programming for non tech entrepreneursProgramming for non tech entrepreneurs
Programming for non tech entrepreneurs
Rodrigo Gil
 

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

Apache Flink Adoption at Shopify
Apache Flink Adoption at ShopifyApache Flink Adoption at Shopify
Apache Flink Adoption at Shopify
 
Amit Bhandari
Amit BhandariAmit Bhandari
Amit Bhandari
 
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
 
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
 
Introduction to interactive data visualisation using R Shiny
Introduction to interactive data visualisation using R ShinyIntroduction to interactive data visualisation using R Shiny
Introduction to interactive data visualisation using R Shiny
 
Integrating Existing C++ Libraries into PySpark with Esther Kundin
Integrating Existing C++ Libraries into PySpark with Esther KundinIntegrating Existing C++ Libraries into PySpark with Esther Kundin
Integrating Existing C++ Libraries into PySpark with Esther Kundin
 
PyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web ApplicationsPyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web Applications
 
C tutorials
C tutorialsC tutorials
C tutorials
 
License Plate Recognition System using Python and OpenCV
License Plate Recognition System using Python and OpenCVLicense Plate Recognition System using Python and OpenCV
License Plate Recognition System using Python and OpenCV
 
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
 
Python Linters at Scale.pdf
Python Linters at Scale.pdfPython Linters at Scale.pdf
Python Linters at Scale.pdf
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
More about PHP
More about PHPMore about PHP
More about PHP
 
Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...
Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...
Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...
 
Larson and toubro
Larson and toubroLarson and toubro
Larson and toubro
 
MS Day EPITA 2010: Visual Studio 2010 et Framework .NET 4.0
MS Day EPITA 2010: Visual Studio 2010 et Framework .NET 4.0MS Day EPITA 2010: Visual Studio 2010 et Framework .NET 4.0
MS Day EPITA 2010: Visual Studio 2010 et Framework .NET 4.0
 
Kyrylo Cherneha "C++ & Python Interaction in Automotive Industry"
Kyrylo Cherneha "C++ & Python Interaction in Automotive Industry"Kyrylo Cherneha "C++ & Python Interaction in Automotive Industry"
Kyrylo Cherneha "C++ & Python Interaction in Automotive Industry"
 
Programming for non tech entrepreneurs
Programming for non tech entrepreneursProgramming for non tech entrepreneurs
Programming for non tech entrepreneurs
 
Gerrit Analytics applied to Android source code
Gerrit Analytics applied to Android source codeGerrit Analytics applied to Android source code
Gerrit Analytics applied to Android source code
 
Python ml
Python mlPython ml
Python ml
 

More from LogeekNightUkraine

More from LogeekNightUkraine (20)

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

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

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