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, so I'll summarize what I've researched. However, the editor does not have to be Visual Studio Code.
We have confirmed in the following environment. CMake The following installation method 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/).
Mingw-w64 is the Windwos 64bit version of the free C ++ compiler g ++. I'm not sure about this either, so please ask Mr. Google.
Download the installer etc. from the Download page. If you are in an online environment, install it with the installer MinGW-W64-install.exe
. If you are in an offline environment, install and extract the 7z format file.
In this article, the placement of g ++. Exe
is as follows: D: \ UserProgram \ mingw-w64 \ x86_64-8.1.0-posix-seh-rt_v6-rev0 \ mingw64 \ bin \ g ++. Exe
Next, add the above folder to the environment variable Path. To add with Powershell
Per user
> $envPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
> $envPath += ";D:\UserProgram\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin" #←g++.The path that contains the exe
> [System.Environment]::SetEnvironmentVariable("Path", $envPath, "User")
To set on a per-terminal basis, replace " User "
on the first and third lines with " Macine "
. In this case, you need administrator privileges.
Terminal unit (administrator authority required)
> $envPath = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
> $envPath += ";D:\UserProgram\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin" #←g++.The path that contains the exe
> [System.Environment]::SetEnvironmentVariable("Path", $envPath, "Machine")
If you are in an online environment, install with pip
.
Powershell
> pip install pybind11
For offline environment
Download pybind11-xyz-py2.py3-none-any.wh
(x, y, z are numbers) from the PyPI Download Page Then install with pip
.
Powershell
>pip install download destination/pybind11-2.5.0-py2.py3-none-any.whl #2.5.Replace 0 with 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. For offline environments, 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]
.
From the cmake_exmple
folder you downloaded earlier, copy pybind11
, src
, and CMakeLists.txt
to an appropriate folder (hereinafter referred to as project_root
).
Folder structure
project_root
├ pybind11
│ ├ docs
│ ├ include
│ │ └pybind11 #The contents of this folder
│ │ └ ・ ・ ・# C:\ProgramData\Anaconda3\include\Replace with the contents of pybind11
│ └ ・ ・ ・
├ src
│ └main.cpp
└ CMakeLists.txt
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)
}
project_root/CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12)
project(cmake_example)
add_subdirectory(pybind11)
pybind11_add_module(cmake_example src/main.cpp)
Open project_root
in Visual Studio Code, go to the terminal withCtrl + @
and do the following:
Powershell
> mkdir build #Create a build destination folder
> cd build #Move to build destination
> cmake -G "MinGW Makefiles" .. #Configure and Generate
> cmake --build .. #Build
If successful, cmake_example.cp37-win_amd64.pyd
will be created in the build
folder. Let's try it in the previous terminal.
Powershell
> python #Launch python
>>> from cmake_example import add
>>> add(1,2)
3
>>> exit() #Return to Powershell
If you can call the ʻadd` function, you are successful.
Thank you for reading to the end.
I've only used the official sample in this article, 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. Also, I'd like to investigate how to use the VS Code extension CMake Tools soon and write an article.
I referred to the following article.
-[Windows MinGW Development Environment](https://www.torutk.com/projects/swe/wiki/Windows_MinGW Development Environment) -[Easy] How to install MinGW-w64 [Many images]
-Introduction to CMake -[How to use Cmake (1)](https://qiita.com/shohirose/items/45fb49c6b429e8b204ac#cmake%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6 % E3% 81% BF% E3% 82% 8B) -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 -Error workaround 1 -Error workaround 2 (unconfirmed)