网站首页 > 技术文章 正文
Python 提供了多种方式与 C/C++ 语言进行交互使用,以下是几种常见的方法:
1. Python 使用 C/C++ 扩展库:
1.1 使用内置的 ctypes 模块:可以用于调用动态链接库(.so 文件)中的 C/C++ 函数。可以使用 ctypes 模块加载 C/C++ 编写的动态链接库,并使用 ctypes 提供的函数指针类型和函数调用方式来调用其中的函数。
// example.c
// 编译生成动态链接库
// gcc -fPIC -shared example.c -o example.so
#include <stdio.h>
#include <stdlib.h>
int add(int a, int b)
{
return a + b;
}
# test.py
# 运行测试脚本
# python test.py
from ctypes import CDLL
example = CDLL('./example.so')
print(example.add(1, 2))
1.2 使用 Ctypesgen:Ctypesgen 是一个 Python 脚本,可以根据 C/C++ 头文件自动生成 Python 的 ctypes 代码。你只需要提供 C/C++ 头文件,然后运行 Ctypesgen 脚本,即可生成对应的 Python 模块。
// example.h
// 依据 C 语言头文件生成 Python 接口文件
// ctypesgen -a -l _example example.h -o example.py
#ifndef EXAMPLE_H
#define EXAMPLE_H
int add(int a, int b);
#endif // EXAMPLE_H
// example.c
// 生成共享库
// gcc --share -fPIC example.c -o _example.so
int add(int a, int b)
{
return a + b;
}
# test.py
# 运行测试脚本
# python test.py
import example
print(example.add(1, 2))
1.3 使用 SWIG(Simplified Wrapper and Interface Generator):SWIG 是一个开源工具,可以自动生成 Python 和 C/C++ 之间的接口代码。你可以使用 SWIG 编写一个接口描述文件,然后使用 SWIG 生成对应的 Python 模块,使得 Python 可以直接调用 C/C++ 函数。
// example.c
int add(int a, int b) {
return a + b;
}
// example.i
// 依据接口描述文件生成 example_wrap.c 和 example.py 文件
// swig -python example.i
// 根据 example_wrap.c 文件生成 _example*.so 共享库
// gcc -shared -fPIC `python3-config --includes` example_wrap.c -o _example`python3-config --extension-suffix`
%module example
%{
#include "example.c"
%}
%include "example.c"
# test.py
# 运行测试脚本
# python test.py
import example
print(example.add(1, 2))
1.4 使用 Pybind11:Pybind11 是一个开源库,可以用于将 C++ 代码包装成 Python 模块。你可以使用 Pybind11 编写一个 C++ 扩展模块,然后 Python 中导入和使用该模块。
// exmpale.cpp
// 编译生成动态链接库
// g++ -std=c++11 -shared -fPIC `python -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix`
#include <pybind11/pybind11.h>
namespace example
{
int add(int i, int j)
{
return i + j;
}
}
int sub(int i, int j)
{
return i - j;
}
PYBIND11_MODULE(example, m)
{
m.doc() = "pybind11 example plugin";
m.def("add", &example::add, "A function that two numbers adds ");
m.def("sub", &sub, "A function that two numbers sub");
}
# test.py
# 运行测试脚本
# python test.py
import example
print(example.add(1, 2))
print(example.sub(4, 2))
2. Python 生成 C/C++ 扩展库:
2.1 使用 Cython:Cython 是一个将 Python 代码转换为 C/C++ 扩展模块的工具。你可以使用 Cython 编写一个扩展模块,其中可以包含 C/C++ 的代码,通过 C/C++ 编译器进行编译。生成的代码经过优化,可以获得接近原生 C/C++ 代码的执行性能,并且可以直接在 Python 中导入和使用该扩展模块。
# example.py
# 编译生成动态链接库
# cythonize example.py -i
def fib(n):
"""Print the Fibonacci series up to n."""
a, b = 0, 1
while b < n:
print(b, end=' ')
a, b = b, a + b
# test.py
# 运行测试脚本
# python test.py
import example
print(example.fib(9))
以上是一些常见的方法,你可以根据具体的需求选择合适的方法与 C/C++ 语言进行交互使用。
猜你喜欢
- 2024-12-23 如何在你的项目中混合 Rust 和 Python
- 2024-12-23 一秒开挂!纯 Python 开发 Web 应用
- 2024-12-23 用 Python 与 Windows 交互 - Pywin32库
- 2024-12-23 交互式环境(Python Shell)编写Python代码
- 2024-12-23 这个用Python编写的大数据测试工具,我给100分
- 2024-12-23 Python与Mysql交互库(持续更新)
- 2024-12-23 Python解释器和交互模式
- 2024-12-23 有用的 Python 提示和技巧 — #5
- 2024-12-23 简单学Python——做一个可交互的图(结合ipywidgets库)
- 2024-12-23 云计算:Python与AWS S3交互
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)