I’m using pybind11 to create cross platform bindings to the c++ library named piper-phonemize.
The library depends on 2 native libraries – onnxruntime and espeak-ng.
There’s cmake file which produce the needed shared libraries.
However, I cannot find a simple way to include these shared libraries in Windows/Linux/macOS.
I use pyproject.toml and setup.py
I would like to place the native pybind11 extension along with the shared libraries in the package folder. Is there a simple way to include them in a way that the extension will be able to load them once I import the extension?
Thanks
1
To achieve the goal if we use Windows 11 (my case) you can first install the necessary tools such as CMake: https://cmake.org/download/ (This can allow you to run the GUI of CMake) https://cmake.org/cmake/help/book/mastering-cmake/chapter/Getting%20Started.html#running-the-cmake-gui
Using the pybind11 library will require a setuptools build script and a build command from CMD, my file structure is the below:
python spythoncompilation.py build
To avoid getting an error on source code I added the full path to the script nevertheless the python3.8 installation had the pybind11 library previously installed specifically to that installation (not a global installation):
Last you use python setuptools and build the libraries:
#pythoncompilation.py
from setuptools import setup, Extension
from pybind11.setup_helpers import Pybind11Extension, build_ext
ext_modules = [
Pybind11Extension("mylib1", ["C:\Users\jbsid\AppData\Local\Programs\Python\Python38\s\mylib1.cpp"]),
Pybind11Extension("mylib2", ["C:\Users\jbsid\AppData\Local\Programs\Python\Python38\s\mylib2.cpp"]),
]
setup(
name="mylibs",
version="0.0.1",
ext_modules=ext_modules,
cmdclass={"build_ext": build_ext},
)
The final result:
You can as well generate the project for Visual Studio with CMake GUI:
If you do it with CMake GUI you can use this list CMake as reference:
cmake_minimum_required(VERSION 3.4)
project(mylibs)
cmake_policy(SET CMP0148 NEW)
set(pybind11_DIR "C:/Users/jbsid/AppData/Local/Programs/Python/Python38/Lib/site-packages/pybind11/share/cmake/pybind11")
find_package(pybind11 REQUIRED)
#add_subdirectory(pybind11)
pybind11_add_module(mylib1 mylib1.cpp)
pybind11_add_module(mylib2 mylib2.cpp)
Result (to include them all you can create a setup.py or package with PyInstaller or similar):