一、动态库文件生成

源文件hello.c

  1. #include "hello.h"
  2. #include <stdio.h>
  3.  
  4. void hello(const char *name)
  5. {
  6. printf("Hello %s!\n", name);
  7. }
  8.  
  9. int factorial(int n)
  10. {
  11. if (n < 2)
  12. return 1;
  13. return factorial(n - 1) * n;
  14. }
  15.  
  16. /* Compute the greatest common divisor */
  17. int gcd(int x, int y) {
  18. int g = y;
  19. while (x > 0) {
  20. g = x;
  21. x = y % x;
  22. y = g;
  23. }
  24. return g;
  25. }

  

头文件hello.h

  1. #ifndef _HELLO_H_
  2. #define _HELLO_H_
  3.  
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7.  
  8. void hello(const char *);
  9. int factorial(int n);
  10. int gcd(int x, int y);
  11.  
  12. #ifdef __cplusplus
  13. }
  14. #endif
  15. #endif

结构体如果放在.h文件中和放在.c中写法没有区别,且重复定义会报错。

如果使用了c++特性(.c文件需要是.cpp文件),.h头需要对应声明,如下结构会更保险,

  1. #ifndef __SAMPLE_H__
  2. #define __SAMPLE_H__
  3.  
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7.  
  8. /* 声明主体 */
  9.  
  10. #ifdef __cplusplus
  11. }
  12. #endif
  13.  
  14. #endif

编译so动态库

  1. gcc -shared -fPIC -o libhello.so hello.c

此时可以看到so文件于文件夹下。

二、使用python调用c函数

尝试使用ctypes模块载入库,并调用函数,

  1. from ctypes import cdll
  2.  
  3. libhello= cdll.LoadLibrary("./libhello.so")
  4. libhello.hello('You')
  5.  
  6. res1 = libhello.factorial(100)
  7. res2 = libhello.gcd(100, 2)
  8. print(libhello.avg)
  9. 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代码_上_用法讲解的更多相关文章

  1. 『Python CoolBook』使用ctypes访问C代码_下_demo进阶

    点击进入项目 这一次我们尝试一下略微复杂的c程序. 一.C程序 头文件: #ifndef __SAMPLE_H__ #define __SAMPLE_H__ #include <math.h&g ...

  2. 『Python CoolBook』Cython

    github地址 使用Cython导入库的话,需要一下几个文件: .c:C函数源码 .h:C函数头 .pxd:Cython函数头 .pyx:包装函数 setup.py:python 本节示例.c和.h ...

  3. 『Python CoolBook』C扩展库_其一_用法讲解

    不依靠其他工具,直接使用Python的扩展API来编写一些简单的C扩展模块. 本篇参考PythonCookbook第15节和Python核心编程完成,值得注意的是,Python2.X和Python3. ...

  4. 『Python CoolBook』Cython_高效数组操作

    数组运算加速是至关科学计算重要的领域,本节我们以一个简单函数为例,使用C语言为python数组加速. 一.Cython 本函数为一维数组修剪最大最小值 version1 @cython.boundsc ...

  5. 『Python CoolBook』C扩展库_其二_demo演示

    点击进入项目 C函数源文件 /* sample.c */ #include "sample.h" /* Compute the greatest common divisor */ ...

  6. 『Python CoolBook』C扩展库_其三_简单数组操作

    点击进入项目 这里的数组要点在于: 数组结构,array.array或者numpy.array 本篇的数组仅限一维,不过基础的C数组也是一维 一.分块讲解 源函数 /* Average values ...

  7. 『Python CoolBook』C扩展库_其四_结构体操作与Capsule

    点击进入项目 一.Python生成C语言结构体 C语言中的结构体传给Python时会被封装为胶囊(Capsule), 我们想要一个如下结构体进行运算,则需要Python传入x.y两个浮点数, type ...

  8. 『Python CoolBook』C扩展库_其五_C语言层面Python库之间调用API

    点击进入项目 一.C层面模块添加API 我们仍然操作如下结构体, #include <math.h> typedef struct Point { double x,y; } Point; ...

  9. 『Python CoolBook』C扩展库_其六_从C语言中调用Python代码

    点击进入项目 一.C语言运行pyfun的PyObject对象 思路是在C语言中提供实参,传给python函数: 获取py函数对象(PyObject),函数参数(C类型) 获取GIL(PyGILStat ...

随机推荐

  1. 解决IE浏览器兼容问题的一行代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. spring拦截器-过滤器的区别

    1.  理解 拦截器 :是在面向切面编程的时候,在你的 service 或者一个方法前调用一个方法,或者在方法后调用一个方法:比如动态代理就是拦截器的简单实现,在你调用方法前打印出字符串(或者做其它业 ...

  3. docker+rabbitmq的安装

    docker pull rabbitmq:management docker run -d -p : -p : -p : -p : -p : -v /data/rabbitmq-data/:/var/ ...

  4. MySql 创建索引原则

    https://blog.csdn.net/csdnones/article/details/50412603 为了使索引的使用效率更高,在创建索引时,必须考虑在哪些字段上创建索引和创建什么类型的索引 ...

  5. [js]js中类的继承

    凡事总有个开端,也有个tag节点(里程碑).阶段性的划分总结,是一种对精神的慰藉,否则精神就像野马一样,会放弃,会累死. 继承: 子类原型指向父类一个实例 类的继承-模拟系统类 Object -> ...

  6. Windows 下运行Makefile文件

    下载并安装Microsoft Visual Studio2017 配置环境变量: 计算机右击-属性-高级系统设置-环境变量-选择Path编辑-添加nmake的路径: D:\Microsoft Visu ...

  7. beego 初体验 - 基础模块 - config, httplibs, logs

    beego 的基础模块支持了一些web开发常用的功能. 配置,http操作库,日志 配置模块: 这是我的配置文件 如何读取: httplibs:这是一个利用 httplibs 发起 get 请求的示例 ...

  8. [转载]lib和dll文件的区别和联系

    出处:https://blog.csdn.net/weiaipan1314/article/details/52252478 什么是lib文件,lib和dll的关系如何 (2008-04-18 19: ...

  9. C语言实例:数组与字符串

    数组: #include <stdio.h> #include <stdlib.h> #define ARRAY_SIZE(Array) (sizeof (Array) / s ...

  10. Shell 常用技巧

    Shell 常用技巧 echo $RANDOM | cksum | cut -c - openssl rand -base64 | cksum | cut -c - date +%N | cut -c ...