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
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.
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?