Execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people) Environment construction

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).

0. Environment

We have confirmed in the following environment. The installation method below CMake is explained in this article.

1. Environment construction

1.1. Installing CMake

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.

1.2. Installation of msvc

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.

1.3. Installation of Pybind 11

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.)

2. I'll try it (■ 1 ■)

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).

  1. Select Debug with Cmake: Select Variant
  2. Select Visal Studio Build Tools 2019 Release --amd64 in Cmake: Select a Kit
  3. Run 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.

3. Troubleshooting

3.1. Configure and Genarate

~~ 3.1.1. 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. At build time

~~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).

4. Conclusion

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.

reference

I referred to the following article.

Commentary on CMake

-Introduction to CMake -How to use Cmake (1) -I tried using CMake (2) A little more decent project

How to use CMake Tools

How to use Pybind 11

-Official sample -Use C ++ functions from python with pybind11 -How to execute C ++ code from Python using pybind11

Recommended Posts

Execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people) Environment construction
Execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people) Debugging
Build an environment to execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people)
Python development environment with Windows + Anaconda3 + Visual Studio Code
Python development environment with Windows + Python + PipEnv + Visual Studio Code
Use C ++ functions from python with pybind11
Build Python development environment with Visual Studio Code
OpenJTalk on Windows10 (Speak Japanese with Python from environment construction)
Set up a Python development environment with Visual Studio Code
Enable the virtualenv Python virtual environment for Visual Studio Code
Build Python3 for Windows 10 on ARM with Visual Studio 2019 (x86) on Windows 10 on ARM
Execute Python code from C # GUI
Try to create a python environment with Visual Studio Code & WSL
Python project environment construction procedure (for windows)
VScode environment construction (Windows10, Python, C ++, C, Git)
Use Jupyter Notebook with Visual Studio Code on Windows 10 + Python + Poetry + pyenv-win
Create a Python development environment on Windows (Visual Studio Code remote WSL).
From re-environment construction of Python to graph drawing (on visual studio code)
Collecting information from Twitter with Python (Environment construction)
VS Code + Azure Functions + Python environment construction procedure
Python (Windows 10) Virtual Environment / Package with VS Code
Wrap C with Cython for use from Python
Django with Python Tools 2.2 for Visual Studio (PTVS 2.2)
Image Processing with Python Environment Setup for Windows
Settings for Python coding in Visual Studio Code
Wrap C ++ with Cython for use from Python
From Python environment construction to virtual environment construction with anaconda
[Visual Studio Code] [Python] Tasks.json + problemMatcher settings for Python
Character code for reading and writing csv files with python ~ windows environment ver ~
Easy Python data analysis environment construction with Windows10 Pro x VS Code x Docker
I customized it with Visual Studio Code (mainly for python), so I will summarize it
python windows environment construction
Make Visual Studio Code autocomplete for python external libraries
A real way for people using python 3.8.0-2 from windows to work with multibyte characters
Environment construction for those who want to study python easily with VS Code (for Mac)
Python with VS Code (Windows 10)
Python environment construction (Windows10 + Emacs)
Python environment construction For Mac
Prepare Python development environment with Mac + Windows + VisualStudio Code (Windows version)
From environment construction to deployment for flask + Heroku with Docker
[Visual Studio Code] [Python] [Windows] Support for garbled Japanese characters in Python in VS Code task / debug output
Python3 environment construction (for beginners)
Prepare Python development environment with Mac + Windows + VisualStudio Code (Mac version)
Install python and Visual Studio Code on windows10 (April 2020 version)
Python environment construction under Windows7 environment
Create a Python environment for professionals in VS Code on Windows
Create a Python execution environment for Windows with VScode + Remote WSL
Building a Windows 7 environment for getting started with machine learning with Python
From building a Python environment for inexperienced people to Hello world
How to connect to Cloud Firestore from Google Cloud Functions with python code
About the procedure for linking Visual Studio Code for Windows and WSL
Pass a list by reference from Python to C ++ with pybind11
Build jupyter notebook environment with Visual Studio Code (VS Code) Mac version
LaTeX and R (a little Python) environment construction with SublimeText3 (Windows)
Python environment construction memo on Windows 10
Java with Visual Studio Code (Part 2)
Get started with Python! ~ ① Environment construction ~
Call C from Python with DragonFFI
Anaconda python environment construction on Windows 10
[Python3] Development environment construction << Windows edition >>
Tips for calling Python from C