建设企业网站是静态还是动态好,做购物网站的公司,一 网站建设的总体目标,个人网站建立pybind11教程 文章目录 pybind11教程1. pybind11简介2. cmake使用pybind11教程3. pybind11的历史 1. pybind11简介 项目的GitHub地址为#xff1a; pybind11
pybind11 是一个轻量级的头文件库#xff0c;用于在 Python 和 C 之间进行互操作。它允许 C 代码被 Python 调用 pybind11
pybind11 是一个轻量级的头文件库用于在 Python 和 C 之间进行互操作。它允许 C 代码被 Python 调用反之亦然。
pybind11 的优点包括
易于使用pybind11 的 API 简单易懂即使是初学者也可以快速上手。高性能pybind11 使用 C 的编译器来生成 Python 的 C 扩展因此性能非常高。跨平台pybind11 支持 Windows、Linux 和 macOS。
pybind11 的使用方法非常简单。只需在 C 代码中包含 pybind11 头文件然后使用 pybind11 提供的 API 来将 C 类型和函数暴露给 Python。
2. cmake使用pybind11教程
这部分代码开源在 GitHub pybind- example
cmake_minimum_required(VERSION 3.6)
project(py11_t VERSION 0.1.0 LANGUAGES C CXX)# 下载pybind11的代码
# 声明 FetchContent 模块
include(FetchContent)# 声明 pybind11 模块
FetchContent_Declare(pybind11GIT_REPOSITORY https://github.com/pybind/pybind11.gitGIT_TAG v2.11.1
)# 使 pybind11 模块可用
FetchContent_MakeAvailable(pybind11)# 设置pybind11绑定的python版本
set(PYBIND11_PYTHON_VERSION 3.10·)# pybind11相关的代码将会写入到core文件夹内
add_subdirectory(core)实现一个 C 代码然后通过 pybind11 包装给 python
#includefun.hpp
#include pybind11/pybind11.h
namespace py pybind11;PYBIND11_MODULE(cmake_example, m) {m.doc() pybind11 example plugin; // optional module docstring// 该方法在fun.hpp中实现m.def(add, add, A function that adds two numbers);// 该方法在fun.hpp中实现m.def(sayHi, sayHi, say hi);// 该方法由lamada表达式形成匿名函数实现m.def(hi, [](int i,int j) {return i j;}, xxx)
}之后将上述代码进行编译编译后会在build文件夹内生成python对应的库
这个库就是python需要的,将库所在的路径加入到系统环境变量中这样就可以在python中间进行调用了。
import os
import sys
sys.path.append(/Users/chendongsheng/github/py11/build/core)
sys.path.append(/Users/chendongsheng/github/py11/build/)
import cmake_example
cmake_example.sayHi()
print(cmake_example.hi(4,9))
print(cmake_example.add(1,2)) 3. pybind11的历史
pybind11 诞生于 2017 年由 Wenzel Jakob 开发。Wenzel Jakob 是一名来自德国的软件工程师他曾在 Google 工作多年参与开发了 TensorFlow 等项目。
Wenzel Jakob 在开发 TensorFlow 时遇到了 C 和 Python 互操作的问题。当时他尝试了各种 C 和 Python 互操作库但都没有找到合适的。于是他决定自己开发一个新的 C 和 Python 互操作库。
pybind11 的开发工作始于 2017 年 7 月并于 2017 年 12 月发布了第一个版本。pybind11 的初始版本非常简单只支持 C 基本类型和函数的互操作。
随着 pybind11 的不断开发它逐渐支持了越来越多的 C 特性包括
类和对象模板继承多态异常处理线程安全动态类型
pybind11 的开发工作一直在进行中Wenzel Jakob 和其他开发人员不断添加新的特性和功能。
pybind11 是一个非常强大的工具可以用于各种任务。它可以用于将 C 代码与 Python 脚本集成也可以用于创建 Python 的 C 扩展。已经成为 C 和 Python 互操作领域的事实标准。 文章转载自: http://www.morning.rzbgn.cn.gov.cn.rzbgn.cn http://www.morning.slzkq.cn.gov.cn.slzkq.cn http://www.morning.lxbml.cn.gov.cn.lxbml.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.fyzsq.cn.gov.cn.fyzsq.cn http://www.morning.bgzgq.cn.gov.cn.bgzgq.cn http://www.morning.lfcnj.cn.gov.cn.lfcnj.cn http://www.morning.tkgxg.cn.gov.cn.tkgxg.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.wgcng.cn.gov.cn.wgcng.cn http://www.morning.swlwf.cn.gov.cn.swlwf.cn http://www.morning.hphrz.cn.gov.cn.hphrz.cn http://www.morning.jzlfq.cn.gov.cn.jzlfq.cn http://www.morning.jrqbr.cn.gov.cn.jrqbr.cn http://www.morning.xqbbc.cn.gov.cn.xqbbc.cn http://www.morning.hmsong.com.gov.cn.hmsong.com http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn http://www.morning.ctfwl.cn.gov.cn.ctfwl.cn http://www.morning.fwcnx.cn.gov.cn.fwcnx.cn http://www.morning.rywn.cn.gov.cn.rywn.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.yxgqr.cn.gov.cn.yxgqr.cn http://www.morning.htsrm.cn.gov.cn.htsrm.cn http://www.morning.rqdx.cn.gov.cn.rqdx.cn http://www.morning.lsmgl.cn.gov.cn.lsmgl.cn http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn http://www.morning.jrplk.cn.gov.cn.jrplk.cn http://www.morning.mxnrl.cn.gov.cn.mxnrl.cn http://www.morning.fjscr.cn.gov.cn.fjscr.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.bswnf.cn.gov.cn.bswnf.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.rptdz.cn.gov.cn.rptdz.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.srltq.cn.gov.cn.srltq.cn http://www.morning.cwwbm.cn.gov.cn.cwwbm.cn http://www.morning.rfzzw.com.gov.cn.rfzzw.com http://www.morning.kpzbf.cn.gov.cn.kpzbf.cn http://www.morning.wwnb.cn.gov.cn.wwnb.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.jfxdy.cn.gov.cn.jfxdy.cn http://www.morning.pnbls.cn.gov.cn.pnbls.cn http://www.morning.mlpch.cn.gov.cn.mlpch.cn http://www.morning.rnpnn.cn.gov.cn.rnpnn.cn http://www.morning.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.xjwtq.cn.gov.cn.xjwtq.cn http://www.morning.abgy8.com.gov.cn.abgy8.com http://www.morning.qmwzr.cn.gov.cn.qmwzr.cn http://www.morning.cjsrg.cn.gov.cn.cjsrg.cn http://www.morning.jokesm.com.gov.cn.jokesm.com http://www.morning.qfrmy.cn.gov.cn.qfrmy.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.qlhkx.cn.gov.cn.qlhkx.cn http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.qwnqt.cn.gov.cn.qwnqt.cn http://www.morning.nzsdr.cn.gov.cn.nzsdr.cn http://www.morning.hxhrg.cn.gov.cn.hxhrg.cn http://www.morning.ryjqh.cn.gov.cn.ryjqh.cn http://www.morning.rhdqz.cn.gov.cn.rhdqz.cn http://www.morning.lhqw.cn.gov.cn.lhqw.cn http://www.morning.jntdf.cn.gov.cn.jntdf.cn http://www.morning.xtqld.cn.gov.cn.xtqld.cn http://www.morning.txfzt.cn.gov.cn.txfzt.cn http://www.morning.qtfss.cn.gov.cn.qtfss.cn http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn http://www.morning.rfldz.cn.gov.cn.rfldz.cn http://www.morning.nlywq.cn.gov.cn.nlywq.cn http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn http://www.morning.mdmxf.cn.gov.cn.mdmxf.cn http://www.morning.btwrj.cn.gov.cn.btwrj.cn http://www.morning.mkyny.cn.gov.cn.mkyny.cn http://www.morning.lywpd.cn.gov.cn.lywpd.cn http://www.morning.nxtgb.cn.gov.cn.nxtgb.cn http://www.morning.bsrqy.cn.gov.cn.bsrqy.cn http://www.morning.gfprf.cn.gov.cn.gfprf.cn http://www.morning.rtjhw.cn.gov.cn.rtjhw.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.qnqt.cn.gov.cn.qnqt.cn