This project demonstrates how to integrate a C++ shared library with a Python application using ctypes.
lib.cpp: Contains the C++ code for the shared library.app.py: Python script that loads and uses the shared library.readme.md: This file.
To compile the C++ code into a DLL on Windows, use the following command:
g++ -shared -o lib.dll lib.cppTo compile the C++ code into a shared object on Linux, use the following command:
g++ -shared -o lib.so lib.cppEnsure the compiled shared library (lib.dll on Windows or lib.so on Linux) is in the same directory as app.py. Then, run the Python script:
python app.pyThe C++ library provides the following functions:
int add(int a, int b): Returns the sum ofaandb.int sub(int a, int b): Returns the difference ofaandb.int mul(int a, int b): Returns the product ofaandb.int divide(int a, int b): Returns the quotient ofaandb.
1 + 2 = 3
4 * 5 = 20
6 - 3 = 3
10 / 2 = 5- Ensure you have the necessary compiler and Python environment set up.
- Modify the path to the shared library in
app.pyif it is not in the same directory.