『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 ...
随机推荐
- Redis应用场景说明与部署
Redis简介 REmote DIctionary Server(Redis)是一个基于key-value键值对的持久化数据库存储系统.redis和大名鼎鼎的memcached缓存服务很像,但是red ...
- 消息服务jms
jms是java消息服务的规范,也即接口,activemq是实现.队列模型和发布订阅模型很像,区别就是队列,多个消费者消费不同的消息(这些消费者整体消费完生产者生产的队列的消息),发布订阅模型是多个消 ...
- Mac 报错:-bash: telnet: command not found
解决方法 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/i ...
- python使用MySQLdb实现连接数据库Mysql
python实现连接数据库mysql的步骤: 一.引入MySQLdb 二.获取与数据库的连接 三.执行SQL语句和存储过程 四.关闭数据库连接 1.什么是MySQLdb? MySQLdb是用于pyth ...
- mysql /tmp目录爆满问题的处理
mysql /tmp目录爆满问题的处理 突然收到zabbix告警,说mysql服务器的/目录磁盘空间不足. 登录到服务器,看了下发现100GB的根目录,居然使用了差不多90GB.这台服务器上只跑了一个 ...
- 【Common】NO.81.Note.1.Common.1.002-【文章摘要】
1.0.0 Summary Tittle:[Common]NO.81.Note.1.Common.1.002-[文章摘要] Style:Common Series:Common Since:2018- ...
- JAVA设计模式之静态代理
首先看看什么是代理模式. 一 代理模式 使用一个代理对象将对象包装起来,然后用该代理对象来取代该对象,任何对原始对象的调用都要通过代理,代理对象决定是否以及何时调用原始对象的方法 二 静态模式 要求被 ...
- phpstorm 破解版
原链接https://blog.csdn.net/gu_wen_jie/article/details/79136475
- 0001-20180421-自动化第一章-python基础学习笔记
======================学习python==================介绍: python种类: cpython(*),jpython,ironpython,rubypyth ...
- Angular4的依赖注入