Example of how to embedd Python3 into a C++ application using CMake. Most Python embedding examples are simplistic and do not show how to make a self-contained installer, in other words deploy an application.
This example shows how to:
- Configure Python dependencies for one library and one executable using CMake and the vcpkg package manager.
- Initialize Python in isolated mode to avoid using system installed Python.
- Execute python code within the application.
- Import Python modules both defined internally (in cpp files) and externally (in Python files) and use them in C++.
- Package Python with the application including modules installed via pip.
- Build and test the installer on Windows/Mac/Linux using Github workflows.
Clone the repository and its submodules.
git clone https://github.com/ResearchDaniel/python3-embedded-example.git --recurse-submodules
Configure for your platfrom (VCPKG_TARGET_TRIPLET: x64-windows
or x64-osx
or x64-linux
) using one of the provided CMake presets (msvc-vcpkg
or xcode-vcpkg
or ninja-vcpkg
). This part will download and compile depencies using the vcpkg package manager so it can take some time.
cmake -S python3-embedded-example -B build
-DVCPKG_TARGET_TRIPLET='x64-windows'
-DCMAKE_BUILD_TYPE=Release
--preset msvc-vcpkg
Where -S <path-to-source> and -B <path-to-build>
cmake --build build --config Release --parallel --target ALL_BUILD package