GVKun编程网logo

windows 10 上使用pybind11进行C++和Python代码相互调用(python调用c++程序)

5

这篇文章主要围绕windows10上使用pybind11进行C++和Python代码相互调用和python调用c++程序展开,旨在为您提供一份详细的参考资料。我们将全面介绍windows10上使用py

这篇文章主要围绕windows 10 上使用pybind11进行C++和Python代码相互调用python调用c++程序展开,旨在为您提供一份详细的参考资料。我们将全面介绍windows 10 上使用pybind11进行C++和Python代码相互调用的优缺点,解答python调用c++程序的相关问题,同时也会为您带来AdDuplex:Windows 10 21H1 和 Windows 11 首次进入快速增、Linux/Wine:windows 应用程序启动 windows 控制台命令并在 windows 上等待时在 linux 上不等待:如何修复?、Microsoft Edge 93 在 Windows 11 和 Windows 10 上获得设计、MSMG ToolKit 11.7 支持 Windows 11 22000 和 Windows 10 21H2的实用方法。

本文目录一览:

windows 10 上使用pybind11进行C++和Python代码相互调用(python调用c++程序)

windows 10 上使用pybind11进行C++和Python代码相互调用(python调用c++程序)

本文首发于个人博客https://kezunlin.me/post/8b9c051d/,欢迎阅读!

Interfacing C++ and Python with pybind11 on windows 10

Series

  • Part 1: Interfacing C++ and Python with pybind11 on windows 10
  • Part 2: Interfacing C++ and Python with pybind11 on ubuntu 16.04

Guide

requirements:

  • pybind11 v2.3.dev0
  • python 2.7

install pytest

pip install pytest 

compile


git clone https://github.com/pybind/pybind11.git
cd pybind11
mkdir build
cd build
cmake-gui ..

with options

PYBIND11_CPP_STANDARD /std:c++11 # default c++14
PYTHON_EXECUTABLE C:/Python27/python.exe
CMAKE_INSTALL_PREFIX C:/Program Files/pybind11

compile with VS 2015 with x64 Release

install to C:\Program Files\pybind11 with only include and share

$ tree .

.
├── include
│   └── pybind11
│       ├── attr.h
│       ├── buffer_info.h
│       ├── cast.h
│       ├── chrono.h
│       ├── common.h
│       ├── complex.h
│       ├── detail
│       │   ├── class.h
│       │   ├── common.h
│       │   ├── descr.h
│       │   ├── init.h
│       │   ├── internals.h
│       │   └── typeid.h
│       ├── eigen.h
│       ├── embed.h
│       ├── eval.h
│       ├── functional.h
│       ├── iostream.h
│       ├── numpy.h
│       ├── operators.h
│       ├── options.h
│       ├── pybind11.h
│       ├── pytypes.h
│       ├── stl.h
│       └── stl_bind.h
└── share
    └── cmake
        └── pybind11
            ├── FindPythonLibsNew.cmake
            ├── pybind11Config.cmake
            ├── pybind11ConfigVersion.cmake
            ├── pybind11Targets.cmake
            └── pybind11Tools.cmake

6 directories, 29 files

Usage

pybind11 (cpp--->python)

  • module: examplelib
  • target: examplelib
  • cpp: example.cpp

example.cpp

#include <pybind11/pybind11.h>

namespace py = pybind11;

int add(int i, int j) {
    return i + j;
}

/*
#include <pybind11/pybind11.h>

namespace py = pybind11;

int add(int i, int j) {
    return i + j;
}

struct Pet {
    Pet(const std::string &name) : name(name) { }
    void setName(const std::string &name_) { name = name_; }
    const std::string &getName() const { return name; }

    std::string name;
};


/*
module: examplelib 
target: examplelib

cpp: example.cpp
*/
PYBIND11_MODULE(examplelib, m)
{
    // optional module docstring
    m.doc() = "pybind11 example plugin";

    // FUNCTIONS
    // expose add function, and add keyword arguments and default arguments
    m.def("add", &add, "A function which adds two numbers", py::arg("i") = 1, py::arg("j") = 2);

    // DATA
    // exporting variables
    m.attr("the_answer") = 42;
    py::object world = py::cast("World");
    m.attr("what") = world;


    // CLASSES
    py::class_<Pet>(m, "Pet")
        .def(py::init<const std::string &>())
        .def("setName", &Pet::setName)
        .def("getName", &Pet::getName);

    /*
    python3 
    > help(examplelib)
    */
}

CMakeLists.txt

cmake_minimum_required (VERSION 2.6)

project (pybind)
enable_language(C)
enable_language(CXX)

find_package(pybind11 CONFIG REQUIRED)
include_directories(${pybind11_INCLUDE_DIRS})
message([MAIN] "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}")

MESSAGE( [Main] " pybind11_INCLUDE_DIRS = ${pybind11_INCLUDE_DIRS}")
MESSAGE( [Main] " pybind11_LIBRARIES = ${pybind11_LIBRARIES}")

#
#   # Create an extension module
#   add_library(mylib MODULE main.cpp)
#   target_link_libraries(mylib pybind11::module)
#
#   # Or embed the Python interpreter into an executable
#   add_executable(myexe main.cpp)
#   target_link_libraries(myexe pybind11::embed)

# method (1): generate `examplelib.pyd`
pybind11_add_module(examplelib example.cpp)

# method (2): generate `examplelib.dll` rename to `examplelib.pyd`
#add_library(examplelib MODULE example.cpp)
#target_link_libraries(examplelib pybind11::module)

MESSAGE( [Main] " pybind11_INCLUDE_DIRS = ${pybind11_INCLUDE_DIRS}")
MESSAGE( [Main] " pybind11_LIBRARIES = ${pybind11_LIBRARIES}")

#add_executable(cpp_use_python cpp_use_python.cpp)
#target_link_libraries(cpp_use_python PRIVATE pybind11::embed)

cmake and config
cmake

build with vs and we get 3 files:

examplelib.lib 
examplelib.exp
examplelib.cp35-win_amd64.pyd

python import examplelib

 python3
Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import examplelib
>>> help(examplelib)
Help on module examplelib:

NAME
    examplelib - pybind11 example plugin

CLASSES
    pybind11_builtins.pybind11_object(builtins.object)
        Pet

    class Pet(pybind11_builtins.pybind11_object)
     |  Method resolution order:
     |      Pet
     |      pybind11_builtins.pybind11_object
     |      builtins.object
     |
     |  Methods defined here:
     |
     |  __init__(...)
     |      __init__(self: examplelib.Pet, arg0: str) -> None
     |
     |  getName(...)
     |      getName(self: examplelib.Pet) -> str
     |
     |  setName(...)
     |      setName(self: examplelib.Pet, arg0: str) -> None
     |
     |  ----------------------------------------------------------------------
     |  Methods inherited from pybind11_builtins.pybind11_object:
     |
     |  __new__(*args, **kwargs) from pybind11_builtins.pybind11_type
     |      Create and return a new object.  See help(type) for accurate signature.

FUNCTIONS
    add(...) method of builtins.PyCapsule instance
        add(i: int = 1, j: int = 2) -> int

        A function which adds two numbers

DATA
    the_answer = 42
    what = ''World''

FILE
    e:\git\car\extra\pybind11\build\release\examplelib.cp35-win_amd64.pyd


>>> p = examplelib.Pet(''kzl'')
>>> print(p)
<examplelib.Pet object at 0x0000025EED9E3D18>
>>> p.getName()
''kzl''
>>> examplelib.add(1,2)
3
>>> examplelib.the_answer
42
>>> examplelib.what
''World''
>>>

embed

example.py


def add(i, j):
    print("hello, pybind11")
    return i+j

class MyMath:

    def __init__(self,name):
        self.name = name
        
    def my_add(self,i,j):
        return i + j
    
    def my_strcon(self,a,b):
        return a + b

cpp_use_python.cpp

#include <pybind11/embed.h>  
#include <iostream>  

namespace py = pybind11;

int main() {
    py::scoped_interpreter python;

    /*
    import sys
    print sys.path
    print "Hello,World!"
    */
    py::module sys = py::module::import("sys");
    py::print(sys.attr("path"));
    py::print("Hello, World!"); // use the Python API

    /*
    import example
    n = example.add(1,2)
    */
    py::module example = py::module::import("example");
    py::object result = example.attr("add")(1, 2);
    int n = result.cast<int>();
    assert(n == 3);
    std::cout << "result from example.add(1,2) = " << n << std::endl;

    /*
    from example import MyMath
    obj = MyMath("v0")
    obj.my_add(1,2)
    */
    py::object MyMath = py::module::import("example").attr("MyMath"); // class
    py::object obj = MyMath("v0"); // class object
    py::object my_add = obj.attr("my_add");// object method
    py::object result2 = my_add(1, 2); // result
    int n2 = result2.cast<int>(); // cast from python type to c++ type
    assert(n2 == 3);
    std::cout << "result from obj.my_add(1,2) = " << n2 << std::endl;

    /*
    from example import MyMath
    obj = MyMath("v0")
    obj.my_strcon("abc","123");
    */

    py::object my_strcon = obj.attr("my_strcon"); // object method
    py::object result3 = my_strcon("abc", "123");
    std::string str3 = result3.cast<std::string>();
    std::cout << "result from obj.my_strcon(abc,123) = " << str3 << std::endl;

    return 0;
}

CMakeLists.txt

cmake_minimum_required (VERSION 2.6)

project (pybind)
enable_language(C)
enable_language(CXX)

find_package(pybind11 CONFIG REQUIRED)
include_directories(${pybind11_INCLUDE_DIRS})

MESSAGE( [Main] " pybind11_INCLUDE_DIRS = ${pybind11_INCLUDE_DIRS}")
MESSAGE( [Main] " pybind11_LIBRARIES = ${pybind11_LIBRARIES}")

add_executable(cpp_use_python cpp_use_python.cpp)
target_link_libraries(cpp_use_python PRIVATE pybind11::embed)

Reference

  • pybind11
  • pybind11 embedding
  • accessing-python-libraries-from-c

History

  • 20180301: created.

Copyright

  • Post author: kezunlin
  • Post link: https://kezunlin.me/post/8b9c051d/
  • Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

AdDuplex:Windows 10 21H1 和 Windows 11 首次进入快速增

AdDuplex:Windows 10 21H1 和 Windows 11 首次进入快速增

AdDuplex 关于 Windows 10 统计数据的最后一份报告是几个月前的。从那以后发生了很多事情。因为Windows 10 2004的比例明显缩水,Windows 10 21H1的比例有所增加。

AdDuplex:Windows 10 21H1 和 Windows 11 首次进入快速增长 - 2021 年 7 月

事实上,现在有 26.6% 的用户在使用 Windows 10 21H1。Windows 10 20H2 的份额从 40.1% 下降到 36.3%,而 2004 年的份额从 40.6% 下降到“仅”24.6%。

有趣的是内部版本用户数量的增加。这里的比例通常最多在 0.4% 和 0.7% 之间波动。但现在仅 Windows 11 就已经拥有 0.9% 的份额,Windows 10 Insider 已经拥有 0.2% 的份额。现在已经总结了旧版本的比例。与 1903 年和 1909 年一起是 11.5%。

Linux/Wine:windows 应用程序启动 windows 控制台命令并在 windows 上等待时在 linux 上不等待:如何修复?

Linux/Wine:windows 应用程序启动 windows 控制台命令并在 windows 上等待时在 linux 上不等待:如何修复?

如何解决Linux/Wine:windows 应用程序启动 windows 控制台命令并在 windows 上等待时在 linux 上不等待:如何修复??

除了一项功能外,我还使用 Wine 成功运行了 Windows 程序。该程序可以设置为在 texlive 发行版中启动 pdflatex,当它启动时,它等待查看结果是否成功,然后复制/移动文件。但是在 Linux 上,我无法安排它像在 Windows 上那样等待 pdflatex 完成,因此过早地复制/移动文件并且 pdflatex 无法找到它们。

有一个对话框可以用来保存任意命令,当我按下应用程序中的 pdflatex 按钮时,该命令将被执行。我将此对话框指向 Windows 上的 C:\texlive\2020\bin\win32\pdflatex.exe 并且它可以工作。除了不等待的问题外,我成功地放置了这个命令,以便在Linux上使用wine运行pdflatex。

Z:/usr/bin/gnome-terminal --wait -- /home/nonnull/bin/runlatex/runlatex.sh "%f"

通过将 runlatex.sh 中的最后一个命令设为 exec /bin/bash,终端保持打开状态,因此我可以看到 pdflatex 的所有输出,并知道它正在使用 runlatex.sh 必须找出的正确文件运行通过将传递给它的 Windows 路径转换为其实际路径。 pdflatex 以各种方式抱怨它在那里创建的文件,然后在运行期间不存在。即使终端是打开的,所以没有完成执行,应用程序抱怨 pdflatex 没有正确完成,甚至在 pdflatex 的输出文本完成向下滚动终端之前。当我省略 exec 时,应用程序的行为不会改变终端立即关闭时的命令 pdflatex 完成。

Wine 是怎么回事,命令在完成运行之前不会保持附加状态?我该怎么办?对可以绕过这个困难的命令或机制有什么建议吗?如果需要的话,我准备编写一个 win32 程序来进行调解。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

Microsoft Edge 93 在 Windows 11 和 Windows 10 上获得设计

Microsoft Edge 93 在 Windows 11 和 Windows 10 上获得设计

WIN11地平线上出现,它的设置为开始滚动10月5日提前计划的推出,微软边缘93已经发布了两个实验功能的支持:菜单和一个全新设计的现代滚动条相匹配的良好作风的视窗 11。

Microsoft Edge 93 具有多项改进和一些新功能。例如,微软已经启用了对类似 Chrome 的标签组的支持,旨在帮助用户有效地查找标签,以便他们可以轻松切换和管理标签。

Microsoft Edge 93 在 Windows 11 和 Windows 10 上获得设计改进

Edge 93 还支持 PDF 文档的“从上次中断的地方继续”。同样,您可以使用将鼠标悬停在支持的视频上时出现的新工具栏,轻松地以画中画模式观看视频。工具栏只是让您在画中画窗口中查看该视频。

垂直选项卡设计已更新为 edge://settings/appearance 中的一个新选项,以隐藏浏览器的标题栏。除了这些改进之外,微软还引入了对新实验性功能的支持:Windows 11 Visual Updates 和 Overlay 滚动条

第一个标志将打开云母效果,这是一种新的 Windows 11 独有的设计材料,可自动将桌面背景的颜色应用于活动窗口。云母效果仅适用于浏览器的标题栏,如下面的屏幕截图所示。

Microsoft Edge 93 在 Windows 11 和 Windows 10 上获得设计改进

它还为 Edge 菜单和上下文菜单启用 Fluent Design 的亚克力效果(部分透明)。如果您在 Windows 11 上启用该标志,它将调整菜单字体的大小,使其看起来更大一些。除了字体变化,它还使一些按钮居中,包括最小化、最大化和关闭。

另一方面,Windows 11 和 Windows 10 都完全支持叠加滚动条。叠加滚动条标志为浏览器的默认滚动条提供了精简设计,并且还支持暗模式。

Microsoft Edge 93 在 Windows 11 和 Windows 10 上获得设计改进

更重要的是,“覆盖滚动条”功能旨在与 Windows 11 应用程序(如设置和资源管理器)的外观和感觉相匹配。

为了尝试这些功能,您需要 Edge 93(稳定版)或更新版本。更新到最新版本后,打开 Edge 标志菜单并启用 Windows 11 视觉更新标志。

要尝试 Edge 93 中的新滚动条,请打开 Edge 的属性选项卡并将以下代码添加到目标字段:

--enable-features=OverlayScrollbar,OverlayScrollbarWinStyle,OverlayScrollbarWinStyleAnimation

微软可能会在未来几个月为 Windows 11 用户引入更多的 Edge 视觉更新。

MSMG ToolKit 11.7 支持 Windows 11 22000 和 Windows 10 21H2

MSMG ToolKit 11.7 支持 Windows 11 22000 和 Windows 10 21H2

我们已经多次报告了 MSMG ToolKit 。一个小工具,除其他外,用于删除 Windows 下的功能。集成更新或驱动程序等等。

使用 MSMG ToolKit 11.7,现在还支持 Windows 11(系统)和 Windows 10 21H2(系统和移除组件)。更改列表很长。这是一个概述:

MSMG ToolKit 11.7 支持 Windows 11 22000 和 Windows 10 21H2

• 使用 Tweak 更新了“Apply Tweaks”功能,以禁用Windows 11 源操作系统的 Windows 11 安装程序的硬件检查。

• bin 文件夹的更新:Windows 11 WADK (v10.0.22000.1)、7-zip v21.03 命令行二进制文件

• 适用于 Windows 11、Windows Server LTSC 2022 的“集成 Microsoft .NET Framework 3.5”功能

• Microsoft .NET Framework 3.5 Pack for MSMG Toolkit for Windows 11, Windows Server LTSC 2022

• Microsoft .NET Framework 4.8 Pack for Windows 7 / Server 2008 R2 和该包也已更新

• 更新了适用于 Windows 11 和 Windows Server LTSC 2022 的“Microsoft Windows Accessible Braille”功能

• 更新“集成多媒体受限编解码器”功能以集成适用于 Windows 10 LTSC 和服务器源操作系统的 Windows 10 杜比编解码器并更新编解码器包。

• 更新开放安全外壳 (SSH) 功能以支持 Windows 11

• 更新了“集成 Microsoft DaRT 工具”功能以支持 Windows 11、Windows Server LTSC 2022 + DebugTools

• 更新删除 Windows 组件功能以支持 Windows 10 1809、1903、1909、2004、20H1、20H2

• 由于错误而禁用 Windows 防火墙 Windows 组件的删除。

• 添加了适用于 Windows 10 的“包含 Windows 图标皮肤”功能。

• 更正了“WIM 管理器- 导出”功能中的错误,如果选择了所有 WIM 索引,则导出失败。

• 已修复函数“WIM Manger- Convert”中的错误,如果选择了所有 WIM 索引,则转换失败。

• 工具包代码优化

关于windows 10 上使用pybind11进行C++和Python代码相互调用python调用c++程序的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于AdDuplex:Windows 10 21H1 和 Windows 11 首次进入快速增、Linux/Wine:windows 应用程序启动 windows 控制台命令并在 windows 上等待时在 linux 上不等待:如何修复?、Microsoft Edge 93 在 Windows 11 和 Windows 10 上获得设计、MSMG ToolKit 11.7 支持 Windows 11 22000 和 Windows 10 21H2的相关信息,请在本站寻找。

本文标签: