CppPyIntegration is a project that showcases various integration methods between C++ and Python programs. It offers examples of invoking C++ code from Python, as well as calling Python functions from C++. These integration techniques include encapsulating C++ code as a Python library and creating subprocesses for C++ execution. The project's architecture is designed to provide clear and reproducible functionalities, enabling seamless interaction between these two programming languages.
Explore the diverse integration possibilities between C++ and Python with the following categories:
1. Invoking C++ Wrapped Package in Python:
- C++ Code Wrapped as Python Library (pybind11): Achieve seamless integration as Python code directly invokes C++ functions through binder.
- C++ Code Wrapped as Python Library (cython): Enhance accessibility by transforming C++ code into a Python module using wrapping and exposure techniques.
2. Invoking Python Class in C++:
- C++ Invokes Python Function: Elevate your C++ application's capabilities by embedding a Python interpreter, enabling it to seamlessly call and utilize Python functions.
3. Python-C++ Subprocess Interaction:
- Python Spawns Subprocess for C++ Execution: Leverage the versatility of Python to initiate and manage C++ programs as separate subprocesses, allowing efficient inter-process communication.
CppPyIntegration/
│
├── CMakeLists.txt # Root-level CMake configuration file
│
├── cpp/
│ ├── CMakeLists.txt # C++ code CMake configuration file
│ ├── src/ # wrapper_lib (C++) invocked in python (Python package wrapped with pybind11)
│ └── py_subprocess.cpp # python subprocess (C++ code)
│
├── python/
│ ├── ext/ # wrapper_lib (C++) invocked in python (Python package wrapped with cython)
│ └── py_module.py # Python module invocked in C++ code
│
├── examples/
│ ├── utils/ # Used to store Python package generated by pybind11 or Cython.
│ ├── python_import_cpp_lib.py # Example1: C++ code wrapped as Python library
│ ├── cpp_call_python_main.cpp # Example2: C++ invokes Python function
│ └── python_spawn_subprocess.py # Example3: Python spawns subprocess with C++ execution
│
└── README.md # Project documentation
Before compiling the project, please ensure that you have a Python 3 environment installed on your computer. Additionally, it's recommended to set up a virtual environment in the project's root directory. Follow the steps below for the setup:
1. Clone project Clone project and its 3rd-party
git clone --recursive https://github.com/GilbertPan97/CppPyIntegration.git
2. Create and activate a virtual environment
macOS and Linux:
python3 -m venv venv
source venv/bin/activate
Windows:
python3 -m venv venv
venv\Scripts\activate
3. Installing python dependencies:
pip install -r requirements.txt
4. Build project and run examples
-
Building C++ Code: Navigate to the
CppPyIntegration/
directory, and follow the instructions inCMakeLists.txt
to build the C++ code. -
Running Examples: Navigate to the
examples/
directory, Run the desired example script using Python or execute the compiled C++ program.
Each integration method has its own example script within the examples/
directory. Follow these steps to try them out:
-
Invoking C++ Wrapped Package in Python:
- Navigate to the
examples/
directory. - Run
python_import_cpp_lib.py
to see Python calling C++ through binding.
- Navigate to the
-
Invoking Python Class in C++:
- Build the C++ program using the instructions in the
examples/
directory. - Run the compiled program to see C++ invoking a Python function.
- Build the C++ program using the instructions in the
-
Python Spawns Subprocess for C++ Execution:
- Run
python_spawn_subprocess.py
in theexamples/
directory to see Python creating a subprocess for C++ execution.
- Run
This project is licensed under the MIT License.
Feel free to explore and adapt the integration methods to your specific needs!
For detailed instructions on each integration method, refer to the README files in their respective directories.