A collection of examples to wrap C++-Code using Cython to be able to use it within Python.
-
Create a virtual Python environment (e.g. using conda)
conda create -n wrapper Python=3.7
-
Inside the created environment install the required packages
conda activate wrapper
conda install -c anaconda pip
pip install -r requirements.txt
-
Go to one of the subdirectories (e.g.
01_simple_c_wrapper
) and follow the steps there (stay in thewrapper
virtual environment).
The code has been tested on MacOS (10.9) and Linux (Ubuntu 18.04 LTS).
Wrapping a function in C that takes a string and prints that string on the console.
References:
Wrapping a function in C++ that takes a string and prints that string on the console.
References:
- Wrapping C libraries with Cython
- Convert str to bytes
- How do I check the operating system in Python?
- sys.platform
- GCC -fPIC option
- cc1plus: warning: command line option “-Wstrict-prototypes” is valid for Ada/C/ObjC but not for C++
Wrapping a function in C++ that takes an array and a number and multiplies that number to each element of the array.
References:
- MemoryViews in Cython
- Numpy in Cython
- How do I check the operating system in Python?
- sys.platform
- GCC -fPIC option
- cc1plus: warning: command line option “-Wstrict-prototypes” is valid for Ada/C/ObjC but not for C++
- Best way to convert numpy array to C++ vector?
- How to initialize std::vector from C-style array?
- Getting Started With Testing in Python
- Best way to assert for numpy.array equality?
- Python test to check instance type
- Using nose2
Wrapping a class in C++ that encapsulates an array and provides various functions to handle that array (sum of its elements, getting array length, getting array itself, multiplying each element by a constant).
References:
- From C++ to Numpy
- From C++ to Numpy (old?)
- MemoryViews in Cython
- C++ in Cython
- How do I check the operating system in Python?
- sys.platform
- GCC -fPIC option
- cc1plus: warning: command line option “-Wstrict-prototypes” is valid for Ada/C/ObjC but not for C++
- Getting Started With Testing in Python
- Best way to assert for numpy.array equality?
- Python test to check instance type
- Using nose2
Wrapping multiple class in C++. Again, we have one class that encapsulates an array. Furthermore, we have another class that takes a constant as input and offers a function which takes a vector class and multiplies each element of the vector class by the constant.
References:
- C++ in Cython
- Nullary Constructor
- How do I check the operating system in Python?
- sys.platform
- GCC -fPIC option
- cc1plus: warning: command line option “-Wstrict-prototypes” is valid for Ada/C/ObjC but not for C++
- Getting Started With Testing in Python
- Best way to assert for numpy.array equality?
- Python test to check instance type
- Using nose2
Wrapping a class in C++ that encapsulates an array and provides various functions to handle that array (sum of its elements, getting array length, getting array itself, multiplying the array with another array). Internally the C++ class uses OpenCV as external library.
References:
- From C++ to Numpy
- From C++ to Numpy (old?)
- MemoryViews in Cython
- C++ in Cython
- How do I check the operating system in Python?
- sys.platform
- GCC -fPIC option
- cc1plus: warning: command line option “-Wstrict-prototypes” is valid for Ada/C/ObjC but not for C++
- Getting Started With Testing in Python
- Best way to assert for numpy.array equality?
- Python test to check instance type
- Using nose2
- How to convert C++ array to opencv Mat
- dot product
- Wrap a c++ class that uses Armadillo by Cython
Creating a package called pymath
containing a regular Python class and a Cython class
that wraps a C++ class. The package can be installed using pip.
References:
- Packaging Python Projects
- Creating a Python package
- Choose an open source license
- Classifiers
- How do I install a Python package with a .whl file?
- Install dependencies from setup.py
- install_requires vs requirements files
- How do I check the operating system in Python?
- sys.platform
- GCC -fPIC option
- cc1plus: warning: command line option “-Wstrict-prototypes” is valid for Ada/C/ObjC but not for C++
- OS detecting makefile
- Getting Started With Testing in Python
- Best way to assert for numpy.array equality?
- Python test to check instance type
- Using nose2
- What is the correct way to share package version with setup.py and the package?
- 5 common patterns to version your Python package
- Distributing Cython modules
- How should I structure a Python package that contains Cython code
- Wrapping a DLL: C++ to Cython to Python