
不用 CMake 或 setuptools如何用 c 命令手动编译 pybind11 扩展模块【免费下载链接】pybind11Seamless operability between C11 and Python项目地址: https://gitcode.com/GitHub_Trending/py/pybind11pybind11 是头文件库header-only不需要链接任何特殊库也没有中间翻译步骤。这意味着在 Linux 或 macOS 上你完全可以跳过 CMake 和 setuptools直接给c命令一行编译出一个 Python 扩展模块。这篇文档对应的官方章节就是 docs/compiling.rst 中的 Building manually 部分配合 docs/basics.rst 的最简示例一条命令即可完成从 C 源码到可import的.so模块的全过程。适用前提是pybind11 已通过pip或conda安装或你手头有 pybind11 源码目录本机有 GCC/Clang 风格编译器和 Python 开发头文件。官方给出的手动编译命令只覆盖 Linux 和 macOSWindows 上没有对应的单条命令示例。准备环境先让python3 -m pybind11这条命令行工具可用。docs/installing.rst 给出几种安装方式其中对这条路径最直接的是pip install pybind11这会把 pybind11 以标准 Python 包形式装进环境python3 -m pybind11 --includes才能取到 pybind11 和 Python 头文件的包含路径。也可以用conda install -c conda-forge pybind11。Linux 上还需要 Python 开发包python-dev或python3-dev否则找不到Python.h这是 docs/basics.rst Compiling the test cases 一节对 Linux 的前置要求。另外注意pip install pybind11[global]这种全局安装会往/usr/local/include/pybind11和/usr/local/share/cmake/pybind11写文件官方建议仅在虚拟环境或pyproject.toml构建场景下使用系统 Python 不要装。写出最小的绑定源码 example.cppdocs/basics.rst 的 Creating bindings for a simple function 给了最小示例把功能函数和绑定代码放进同一个文件example.cpp#include pybind11/pybind11.h namespace py pybind11; int add(int i, int j) { return i j; } PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { m.doc() pybind11 example plugin; // optional module docstring m.def(add, add, A function that adds two numbers); }PYBIND11_MODULE的第一个参数example是模块名不带引号它决定了编译产物的名字和 Python 侧的import名。注意pybind11/pybind11.h内部包含了Python.h所以它必须是源文件里第一个 include。实际项目中实现和绑定代码通常会拆分到不同文件这里为最小示例合在一起。Linux 下用 c 命令编译官方给出的 Linux 编译命令是c -O3 -Wall -shared -stdc11 -fPIC $(python3 -m pybind11 --includes) example.cpp -o example$(python3 -m pybind11 --extension-suffix)各部分的用途-O3 -Wall优化与告警示例中的建议参数-shared -fPIC生成共享库所需的标志$(python3 -m pybind11 --includes)展开为 pybind11 和 Python 头文件两部分的-I路径前提就是 pybind11 已用pip或conda装好$(python3 -m pybind11 --extension-suffix)给出当前 Python 版本的扩展模块后缀如.cpython-311-x86_64-linux-gnu.so保证产物名与 Python 解释器匹配。如果 pybind11 没有通过 pip/conda 安装例如你用的是 git submoduledocs/compiling.rst 的说法是手动指定-I path-to-pybind11/include并配合 Python 自己的包含路径python3-config --includes。docs/basics.rst 对 submodule 场景给出了具体替换方式把命令里的$(python3 -m pybind11 --includes)换成$(python3-config --includes) -Iextern/pybind11/include假设 pybind11 在extern/pybind11。macOS 下只差一个链接标志macOS 的编译命令几乎相同但必须额外加-undefined dynamic_lookup让构建时忽略未解析符号c -O3 -Wall -shared -stdc11 -undefined dynamic_lookup $(python3 -m pybind11 --includes) example.cpp -o example$(python3-config --extension-suffix)为什么可以不在编译期解析这些符号官方 note 解释在 Linux 和 macOS 上扩展模块应该刻意不链接libpython符号会在扩展被加载进 Python 进程时解析。这样做的好处是同一进程里即使存在多个同版本的 Python 安装系统 Python 和某商业软件自带的插件都能工作而避免把第二份 Python 库导入已经含有一份的进程那会导致段错误。可选让命令行工具直接拼出全部标志docs/compiling.rst 提到python3 -m pybind11还能基于python-config生成完整编译标志适合快速试验c $(python3 -m pybind11 --cflags) example.cpp $(python3 -m pybind11 --ldflags) -o example$(python3 -m pybind11 --extension-suffix)更短的形式是--file它会打印出除编译器之外的全部参数包括输出文件名输出文件会放在源码文件旁边c $(python3 -m pybind11 --fileexample.cpp)看 pybind11/commands.py 的实现可以确认细节--cflags输出包含路径、CFLAGS配置值和-stdc17--ldflags在 Linuxposix上输出-fPIC -shared在 macOSdarwin上输出-undefined dynamic_lookup -shared--embed参数则用于构建内嵌解释器的程序而不是扩展模块。注意官方定位这些 helper 面向 Unix 风格编译器GCC/Clangintended for quick tests, not production builds用于快速测试而非生产构建而且 docs/compiling.rst 建议生产构建参考其 CMake 章节里给出的跨平台构建系统以获得更小的二进制。验证模块能否被 Python 加载编译成功后产物就放在当前目录。docs/basics.rst 给出的验证方式是进入交互式 Python 会话确认模块能 import、函数能调用以下为文档示例会话$ python Python 3.9.10 (main, Jan 15 2022, 11:48:04) [Clang 13.0.0 (clang-1300.0.29.3)] on darwin Type help, copyright, credits or license for more information. import example example.add(1, 2) 3能import example并调用example.add(1, 2)得到3说明扩展模块编译、后缀、符号解析全部正确。若 import 报模块找不到通常说明-o后面--extension-suffix展开的后缀与当前解释器不匹配——用python3 -m pybind11 --extension-suffix查一下当前环境期望的后缀即可。限制与下一步官方手动编译章节只给了 Linux 和 macOS 的命令Windows 上没有对应的c单行路径文档中 Windows 的构建都走 CMake Visual Studio 2019。手动编译缺少 CMake 路径里pybind11_add_module自动附加的那些优化隐藏符号可见性、LTO、strip 符号等docs/compiling.rst 明确指出这些标志对加载多个不同 pybind11 版本的模块时有实际意义。命令行 helper--cflags/--ldflags/--file只支持 GCC/Clang且官方定位是快速测试不是生产构建。需要跨平台、可复用的构建时docs/compiling.rst 的主路径是 CMakepybind11_add_module配合pyproject.tomlsetuptools 用户则用 pybind11/setup_helpers.py 里的Pybind11Extension。【免费下载链接】pybind11Seamless operability between C11 and Python项目地址: https://gitcode.com/GitHub_Trending/py/pybind11创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考