I will explain how to build an environment that allows you to call C ++ functions from Python using Pybind11. There weren't many articles for people using Windows and Visual Studio Code (hereinafter VSCode), so I'll summarize what I've researched. Click here for debugging (https://qiita.com/k_maki/items/75bf05e4159be92c0bd9).
We have confirmed in the following environment. The installation method below CMake is explained in this article.
CMake is a tool that makes the settings for building C ++ etc. available to various compilers. However, I'm not sure about it myself, so please see here for details.
Download and install the Windows win64-x64 Installer from the Download Page (https://cmake.org/download/).
Also, install the extension CMake Tools for VS Code in VS Code. If your device is offline, please refer to here.
msvc is a Microsoft C ++ compiler. The C ++ compiler part of Visual Studio.
For the installation method, see here when the device is online, and here when the device is offline.
If you are in an online environment, install with pip
.
Powershell
> pip install pybind11
For offline environment
Download pybind11-x.y.z-py2.py3-none-any.wh
(x, y, z are numbers) from the PyPI Download Page (https://pypi.org/project/pybind11/#files) and then install with pip
.
Powershell
>pip install download destination/pybind11-2.5.0-py2.py3-none-any.whl #2.5.0 part should be read as appropriate
I will also download the official sample for later use. If you are online (and have Git installed), go to the location you want to download on Powershell and
Powershell
git clone --recursive https://github.com/pybind/cmake_example.git
Download as. If you are in an offline environment, press the Clone or download
button in the Official Sample (https://github.com/pybind/cmake_example). ~~ Also, download the pybind11 folder in the same way. ~~
~~ ** [Important] Replace the files under cmake_example/pybind11/include/pybind11
with the files under C: \ ProgramData \ Anaconda3 \ include \ pybind11
. ** The file before replacement seems to be old and compiles, but I get a lot of warnings warning:'void PyThread_delete_key_value (int)' is deprecated [-Wdeprecated-declarations]
. ~~ (Corrected 2021/1. Deleted because no warning is issued at this time.)
From the cmake_exmple
folder you downloaded earlier, copy pybind11
, src
, and CMakeLists.txt
to an appropriate folder (hereinafter referred to as project_root
). ~~ In addition, if Japanese is included in the path name, Configure and Genarate cannot be performed, so please use half-width alphanumeric characters (spaces and symbols are also OK). ~~ (If you use g ++ for the compiler, Japanese is not good, but msvc seems to be OK)
Folder structure
project_root
├ pybind11
├ src
│ └main.cpp
└ CMakeLists.txt
Comment out or delete the #ifdef VERSION_INFO
to #endif
part of main.cpp
because it causes a compile error (I don't know why).
project_root/src/main.cpp
#include <pybind11/pybind11.h>
int add(int i, int j) {
return i + j;
}
namespace py = pybind11;
PYBIND11_MODULE(cmake_example, m) {
(Abbreviation)
m.def("add", &add, R"pbdoc(
Add two numbers
Some other explanation about the add function.
)pbdoc");
(Abbreviation)
//Comment out or delete the following because it will cause a compile error (I do not know why)
//#ifdef VERSION_INFO
// m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
//#else
// m.attr("__version__") = "dev";
//#endif
}
project_root/CMakeLists.txt
cmake_minimum_required(VERSION 3.4...3.18)
project(cmake_example)
add_subdirectory(pybind11)
pybind11_add_module(cmake_example src/main.cpp)
# EXAMPLE_VERSION_INFO is defined by setup.py and passed into the C++ code as a
# define (VERSION_INFO) here.
target_compile_definitions(cmake_example PRIVATE VERSION_INFO=${EXAMPLE_VERSION_INFO})
Open project_root
with VS Code and build using CMake Tools from the command palette (Ctrl + p
).
Debug
with Cmake: Select Variant
Visal Studio Build Tools 2019 Release --amd64
in Cmake: Select a Kit
Cmake: Build
The above can also be set by clicking the VS Code status bar (blue bar at the bottom).
If successful, cmake_example.cp37-win_amd64.pyd
will be created in the ./build/Debug
folder. Open a terminal with Ctrl + @
in VS Code and give it a try.
Powershell
> python #Launch python
>>> from build.Debug.cmake_example import add
>>> add(1,2)
3
>>> exit() #Return to Powershell
If you can call the add
function, you are successful.
The debugging method of the add
function is explained in Debugging, so please refer to that as well.
cmake -G" MinGW Makefiles "..
cannot be done ~~~~ If the path name contains Japanese, Configure and Genarate cannot be performed. The path name should be quiet and half-width alphanumeric characters (spaces and symbols are OK). I want you to support Japanese. ~~ (If you use g ++ for the compiler, Japanese is not good, but msvc seems to be OK)
~~3.2.1. warning: 'void PyThread_delete_key_value(int)' is deprecated [-Wdeprecated-declarations]~~
~~ As I wrote above, replace the files under cmake_example/pybind11/include/pybind11
with the files under C: \ ProgramData \ Anaconda3 \ include \ pybind11
. The file before replacement seems to be old and compiles, but I get a lot of warnings warning:'void PyThread_delete_key_value (int)' is deprecated [-Wdeprecated-declarations]
. ~~ (Corrected 2021/1. Deleted because no warning is issued at this time.)
3.2.2. error: '::hypot' has not been declared If you are using any version of Python released before December 2018, the build will fail with this error. The cause is pyconfig.h, and it is fixed as follows.
Python installation destination/include/pyconfig.h
#define COMPILER "[gcc]"
- #define hypot _hypot ← Delete this line.
#define PY_LONG_LONG long long
#define PY_LLONG_MIN LLONG_MIN
#define PY_LLONG_MAX LLONG_MAX
This issue has been officially discussed and merged after a pull request (actual commit).
Thank you for reading to the end.
This article only used the official sample, but please refer to other people's articles and modify main.cpp
and CMakeLists.txt
.
I'm not familiar with C ++, so it took me a while to be able to do just this. I would like to make my C ++ debut.
I referred to the following article.
-Introduction to CMake -How to use Cmake (1) -I tried using CMake (2) A little more decent project
-Official sample -Use C ++ functions from python with pybind11 -How to execute C ++ code from Python using pybind11