这是之前相关的两篇博文:
本篇给出 C++ 源码编译成二进制文件,并闭源上传至 PyPI 的步骤流程和注意事项,以及对应的文件目录结构和内容,和上面两篇博文会有些区别。
文件目录结构为:
- guan_cpp_project
- src
- cpp
- main.cpp
- guan_cpp
- __init__.py
- cpp
- pyproject.toml
- setup.py
- src
main.cpp 内容示例:
#include <pybind11/pybind11.h>
int add(int a, int b) {
return a + b;
}
PYBIND11_MODULE(guan_cpp_module, m) {
m.doc() = "My C++ extension for Python";
m.def("add", &add, "A function that adds two numbers");
}
__init__.py 内容示例:
from .guan_cpp_module import *
pyproject.toml 内容示例:
[build-system]
requires = ["setuptools", "wheel", "pybind11"]
build-backend = "setuptools.build_meta"
setup.py 内容示例:
from setuptools import setup, Extension
import pybind11
ext_modules = [
Extension(
"guan_cpp.guan_cpp_module", # 包名.模块名
["src/cpp/main.cpp"], # C++ 源文件列表
include_dirs=[pybind11.get_include()], # pybind11头文件
language="c++", # 指定语言为 C++
extra_link_args=["-static-libstdc++"], # 可选静态链接
),
]
setup(
name="guan_cpp", # 项目的名称(用于 pip install)
version="0.0.1", # 版本号
package_dir={"": "src"}, # 指定 Python 包的根目录
packages=["guan_cpp"], # 包的名称(用于 import)
ext_modules=ext_modules, # 指定 C++ 扩展模块
)
文件说明:
- /src/cpp 文件夹为 c++ 程序;/src/guan_cpp 文件夹为 python 程序。
- 在 setup.py 文件中指定了 c++ 模块,实现方法是使用 pybind11 软件包。
- 在当前环境下编译:python -m build 。上传至 PyPI:python -m twine upload dist/*.whl 。这种编译方案只能适配当前设备,其他人可能无法安装。以下内容是创建多个环境,并在多个环境下编译。因为公开的是 C++ 二进制文件,所以编译需要尽可能兼容不同设备。
使用 Conda 创建多个 Python 环境:
conda create --name python36 python=3.6 -y
conda create --name python37 python=3.7 -y
conda create --name python38 python=3.8 -y
conda create --name python39 python=3.9 -y
conda create --name python310 python=3.10 -y
conda create --name python311 python=3.11 -y
conda create --name python312 python=3.12 -y
conda create --name python313 python=3.13 -y
查看 Python 环境:
conda info -e
修改 pip 源(可选):
conda run -n python36 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
conda run -n python37 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
conda run -n python38 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
conda run -n python39 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
conda run -n python310 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
conda run -n python311 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
conda run -n python312 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
conda run -n python313 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
在各个 Python 环境下安装或更新 build:
conda run -n python36 pip install --upgrade build
conda run -n python37 pip install --upgrade build
conda run -n python38 pip install --upgrade build
conda run -n python39 pip install --upgrade build
conda run -n python310 pip install --upgrade build
conda run -n python311 pip install --upgrade build
conda run -n python312 pip install --upgrade build
conda run -n python313 pip install --upgrade build
在各个 Python 环境下编译(Windows系统、Linux系统等):
conda run -n python36 python -m build --wheel
conda run -n python37 python -m build --wheel
conda run -n python38 python -m build --wheel
conda run -n python39 python -m build --wheel
conda run -n python310 python -m build --wheel
conda run -n python311 python -m build --wheel
conda run -n python312 python -m build --wheel
conda run -n python313 python -m build --wheel
在 Linux 系统中编译后需要额外把 whl 文件改成 manylinux 标签:
pip install auditwheel
auditwheel repair dist/*-linux_x86_64.whl
使用以上命令修复后,带有 -manylinux2014_x86_64 标签的 .whl 文件在 wheelhouse 文件夹中。
把所有的 .whl 文件上传到 PyPI:
python -m twine upload dist/*.whl
需要注意的是,如果 C++ 核心代码闭源,那么 .tar.gz 压缩包不要上传,里面包含了 C++ 源码,只上传 .whl 即可。上传至 PyPI 需要注册 PyPI 账号,通过 API token 上传,注册和上传步骤稍微有点繁琐。
在不同平台上测试安装软件包:
pip install guan_cpp
或者
pip install guan_cpp -i https://pypi.python.org/simple
代码测试:
import guan_cpp
print(guan_cpp.add(3, 2))
访问安装后的 site-packages/guan_cpp 软件包文件夹,可以发现无法看到 c++ 源码,只看到 .pyd(windows 系统)或 .io(Linux 系统)的二进制文件。
【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com】