『Python CoolBook』使用ctypes访问C代码_上_用法讲解
一、动态库文件生成
源文件hello.c
#include "hello.h"
#include <stdio.h> void hello(const char *name)
{
printf("Hello %s!\n", name);
} int factorial(int n)
{
if (n < 2)
return 1;
return factorial(n - 1) * n;
} /* Compute the greatest common divisor */
int gcd(int x, int y) {
int g = y;
while (x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
}
头文件hello.h
#ifndef _HELLO_H_
#define _HELLO_H_ #ifdef __cplusplus
extern "C" {
#endif void hello(const char *);
int factorial(int n);
int gcd(int x, int y); #ifdef __cplusplus
}
#endif
#endif
结构体如果放在.h文件中和放在.c中写法没有区别,且重复定义会报错。
如果使用了c++特性(.c文件需要是.cpp文件),.h头需要对应声明,如下结构会更保险,
#ifndef __SAMPLE_H__
#define __SAMPLE_H__ #ifdef __cplusplus
extern "C" {
#endif /* 声明主体 */ #ifdef __cplusplus
}
#endif #endif
编译so动态库
gcc -shared -fPIC -o libhello.so hello.c
此时可以看到so文件于文件夹下。
二、使用python调用c函数
尝试使用ctypes模块载入库,并调用函数,
from ctypes import cdll
libhello= cdll.LoadLibrary("./libhello.so")
libhello.hello('You')
res1 = libhello.factorial(100)
res2 = libhello.gcd(100, 2)
print(libhello.avg)
print(res1, res2)
>>> python hello.py
Hello Y!
<_FuncPtr object at 0x7f9aedc3d430>
0 2
函数hello和我们预想的不一致,仅仅输出了首字母"Y",对于cookbook的其他c函数实际上我也做了导入,但是数据格式c和python是需要转换的,调用起来不是很容易的一件事,本篇重点在于成功导入python,之后的问题之后讲。
『Python CoolBook』使用ctypes访问C代码_上_用法讲解的更多相关文章
- 『Python CoolBook』使用ctypes访问C代码_下_demo进阶
点击进入项目 这一次我们尝试一下略微复杂的c程序. 一.C程序 头文件: #ifndef __SAMPLE_H__ #define __SAMPLE_H__ #include <math.h&g ...
- 『Python CoolBook』Cython
github地址 使用Cython导入库的话,需要一下几个文件: .c:C函数源码 .h:C函数头 .pxd:Cython函数头 .pyx:包装函数 setup.py:python 本节示例.c和.h ...
- 『Python CoolBook』C扩展库_其一_用法讲解
不依靠其他工具,直接使用Python的扩展API来编写一些简单的C扩展模块. 本篇参考PythonCookbook第15节和Python核心编程完成,值得注意的是,Python2.X和Python3. ...
- 『Python CoolBook』Cython_高效数组操作
数组运算加速是至关科学计算重要的领域,本节我们以一个简单函数为例,使用C语言为python数组加速. 一.Cython 本函数为一维数组修剪最大最小值 version1 @cython.boundsc ...
- 『Python CoolBook』C扩展库_其二_demo演示
点击进入项目 C函数源文件 /* sample.c */ #include "sample.h" /* Compute the greatest common divisor */ ...
- 『Python CoolBook』C扩展库_其三_简单数组操作
点击进入项目 这里的数组要点在于: 数组结构,array.array或者numpy.array 本篇的数组仅限一维,不过基础的C数组也是一维 一.分块讲解 源函数 /* Average values ...
- 『Python CoolBook』C扩展库_其四_结构体操作与Capsule
点击进入项目 一.Python生成C语言结构体 C语言中的结构体传给Python时会被封装为胶囊(Capsule), 我们想要一个如下结构体进行运算,则需要Python传入x.y两个浮点数, type ...
- 『Python CoolBook』C扩展库_其五_C语言层面Python库之间调用API
点击进入项目 一.C层面模块添加API 我们仍然操作如下结构体, #include <math.h> typedef struct Point { double x,y; } Point; ...
- 『Python CoolBook』C扩展库_其六_从C语言中调用Python代码
点击进入项目 一.C语言运行pyfun的PyObject对象 思路是在C语言中提供实参,传给python函数: 获取py函数对象(PyObject),函数参数(C类型) 获取GIL(PyGILStat ...
随机推荐
- Python字符串拼接的6种方法(转)
add by zhj: 对于多行字符串连接,第6种连接方法很方便,连接时不会添加额外的空格. 原文:http://www.cnblogs.com/bigtreei/p/7892113.html 1. ...
- autoMapper的介绍
.NET的DTO映射工具AutoMapper 分类: 多层架构 DTO .NET2012-08-11 10:27 2466人阅读 评论(0) 收藏 举报 原文:https://github.com/A ...
- 实验一:C++简单程序设计
[实验一] #2-28 实现一个简单的菜单程序,运行时显示“Menu:A(dd) D(elete) S(ort) Q(uit),Selete one:”提示用户输入.A表示增加,D表示删除,S表示排序 ...
- Android adt-bundle 开发环境的搭建_Linuxs
本文完全是拷贝的: https://www.jb51.net/article/87957.htm 的文章, 有需要请看原文, 拷贝仅用于学习记录. 本文与<利用adt-bundle轻松搭建An ...
- nodejs之querystring(查询字符串)
querystring模块经常用在URL参数的处理,一共有四个方法: 1. stringify (字符串转对象) 2. parse (对象转字符串) 3. escape (对字符串进行URL编码) 4 ...
- make pycaffe时候报错:Makefile:501: recipe for target 'python/caffe/_caffe.so' failed
安装caffe-ssd编译环境的时候报错: python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file ...
- PHP开发微信公众号
PHP开发微信公众号:配置和部署服务器及Token认证 https://zhuanlan.zhihu.com/p/28259840
- Field amqpTemplate in * required a single bean, but 3 were found:
Field amqpTemplate in * required a single bean, but 3 were found: Spring Boot 启动的时候报的错 使用Spring Boot ...
- Python socket粘包解决
socket粘包: socket 交互send时,连续处理多个send时会出现粘包,soket会把两条send作为一条send强制发送,会粘在一起. send发送会根据recv定义的数值发送一个固定的 ...
- 性能测试监控工具nmon详解和分析
性能测试监控工具nmon详解和分析 1.命令安装 1.查看liunx版本版本x86_64_14i 目录:cd /nmon/logs/ 版本x86_64_14i [root@localhost u06] ...