There are many articles that make libraries written in C ++ available from Python using pybind11, I felt that there wasn't much talk about how to publish what I made.
In this article, I will explain the procedure to create a Github repository that can be published based on the template cmake_example
officially provided by pybind. If you follow this procedure to the end, you will have a repository that users can install and start using with just a few commands.
cmake_example is an official template set up to build with CMake. Get this first.
cd work #Enter a suitable working directory
git clone https://github.com/pybind/cmake_example.git
How to create a repository is omitted. Let's assume that the repository name is mylib
. You can freely create .gitignore, but be careful as you will need to combine it with cmake_example later. You can set public / private as you like. Clone if you can.
git clone https://github.com/*****/mylib.git
cd mylib
git submodule add -b master https://github.com/pybind/pybind11.git pybind11
Various downloads are made under the pybind11 /
directory, and .gitmodules
is generated.
Copy the necessary files & directories from the cmake_example /
directory to mylib /
. The minimum required items are as follows.
Rename LICENSE
to LICENSE.pybind
and prepare a separate LICENSE
for your project. As mentioned above, for .gitignore
, merge your own with cmake_example.
Change the part where cmake_example
is written to your own library name.
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12)
project(mylib) <--
add_subdirectory(pybind11)
pybind11_add_module(mylib src/main.cpp) <--
Rewrite the setup
call part at the bottom with your own information.
setup.py
setup(
name='mylib', <-
version='1.0.0', <-
author='oreore', <-
author_email='[email protected]', <-
description='HogeHoge', <-
long_description='', <-
ext_modules=[CMakeExtension('mylib')], <-
cmdclass=dict(build_ext=CMakeBuild),
zip_safe=False,
)
If necessary, add the requirements of the external library to the description as follows.
...
zip_safe=False,
packages=find_packages(),
install_requires=['pillow']
Rewrite the library name to mylib
at the specification of PYBIND11_MODULE
in main.cpp
.
PYBIND11_MODULE(mylib, m) {
After that, create the code as you like and add it to CMakeLists.txt
as needed.
python setup.py build
OK if there are no errors
python setup.py install
Go out of the mylib /
directory, start python, import the library and check the operation. If you check in mylib /
, the files under the current directory may be used instead of the installed files due to the influence of the file search order, so do it outside.
>>> import mylib
>>> mylib.add(1, 2)
>>> 3
To get users to use what you have created, take the following three steps.
For Ubuntu etc.
sudo apt install build-essential cmake
You just have to hit it. If you're a Windows user ..., do your best to have Visual Studio 2019 and CMake installed separately (you also need to get CMake through the path, which is painful).
Ask the user to execute the following command when cloning the repository.
git clone --recursive https://github.com/******/mylib.git
If you do not add --recursive
, pybind11
will not be downloaded and you will not be able to build it, so describe it properly.
python setup.py install
I have you hit. that's all.
If you want to provide not only the C ++ library but also the Python code, change the C ++ library name to _mylib
and load it as import from mylib / __ init__.py
.
Specifically, first, make CMakeLists.txt
as follows.
CMakeLists.txt
project(_mylib)
...
pybind11_add_module(_mylib src/main.cpp)
Then setup.py
setup.py
ext_modules=[CMakeExtension('mylib._mylib')],
This means putting the build artifacts in mylib / _mylib ****
(.so or .pyd).
Then src / main.cpp
src/main.cpp
PYBIND11_MODULE(_mylib, m) {
Finally, prepare the mylib /
directory where the Python source code is placed and write __init__.py
.
mylib/__init__.py
from ._mylib import *
Then add the Python code as you like.
Since I am using CMake, I will generate a solution file for Visual Studio.
Create a build /
directory and run cmake in it.
mkdir build
cd build
cmake .. -G "Visual Studio 16 2019"
Since build / mylib.sln
is generated, develop using this. The build /
directory is included in .gitignore
, so if you want to register solution files etc. in the repository, use a different directory instead of build
.
Recommended Posts