内容表

  • 4. 构建 C/C++ 扩展
    • 4.1. Building C and C++ Extensions with setuptools

上一话题

3. 定义扩展类型:分类话题

下一话题
就业培训     下载中心     Wiki     联络
登录   注册

Log
  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. 扩展和嵌入 Python 解释器
  8. 4. 构建 C/C++ 扩展

4. 构建 C/C++ 扩展 ¶

CPython 的 C 扩展是共享库 (如 .so 文件在 Linux, .pyd 在 Windows),导出 初始化函数 .

要可导入,共享库必须可用于 PYTHONPATH , and must be named after the module name, with an appropriate extension. When using setuptools, the correct filename is generated automatically.

初始化函数带有签名:

PyObject * PyInit_modulename ( void ) ¶

It returns either a fully initialized module, or a PyModuleDef 实例。见 初始化 C 模块 了解细节。

对于采用仅 ASCII 名称的模块,函数必须被命名为 PyInit_<modulename> ,采用 <modulename> 替换模块名称。当使用 多阶段初始化 ,非 ASCII 模块名称是允许的。在这种情况下,初始化函数名称是 PyInitU_<modulename> ,采用 <modulename> 编码使用 Python 的 punycode 编码采用下划线替换连字符。在 Python 中:

def initfunc_name(name):
    try:
        suffix = b'_' + name.encode('ascii')
    except UnicodeEncodeError:
        suffix = b'U_' + name.encode('punycode').replace(b'-', b'_')
    return b'PyInit' + suffix
										

从单个共享库导出多个模块是可能的,通过定义多个初始化函数。不管怎样,导入它们要求使用符号链接或自定义导入器,因为默认情况下只能找到对应文件名的函数。见 在一个库中多个模块 章节在 PEP 489 了解细节。

4.1. Building C and C++ Extensions with setuptools ¶

Python 3.12 and newer no longer come with distutils. Please refer to the setuptools documentation at https://setuptools.readthedocs.io/en/latest/setuptools.html to learn more about how build and distribute C/C++ extensions with setuptools.

内容表

  • 4. 构建 C/C++ 扩展
    • 4.1. Building C and C++ Extensions with setuptools

上一话题

3. 定义扩展类型:分类话题

下一话题

5. 在 Windows 构建 C/C++ 扩展

本页

  • 报告 Bug
  • 展示源

快速搜索

键入搜索术语或模块、类、函数名称。

  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. 扩展和嵌入 Python 解释器
  8. 4. 构建 C/C++ 扩展
  9. 版权所有  © 2014-2026 乐数软件    

    工业和信息化部: 粤ICP备14079481号-1