C 语言 和 python 调用 .so 文件
什么是静态库和动态库, 看一篇博客
http://www.cnblogs.com/skynet/p/3372855.html
现在,我们首先生成.so文件
首先, 我们写一个a.c文件
|
1
2
3
4
5
6
7
8
9
|
#include <stdio.h>void show() { printf("this is a test\n");}int add(int a, int b) { return a + b;} |
然后输入命令
|
1
|
gcc a.c -fPIC -shared -o a.so |
在当前目录下会产生一个a.so文件
其中 -fPIC是position independent code(位置无关代码)的意思
-shared是产生一个可以与其他对象连接来形成一个可执行文件的共享对象的一个参数
首先, python中调用.so库
|
1
2
3
4
5
6
7
|
from ctypes import cdllcur = cdll.LoadLibrary('./a.so')cur.show()print cur.add(1, 2) |
结果如下
|
1
2
|
hello, world!3 |
然后,在C语言中调用.so文件
首先看一组API
|
1
2
3
4
5
|
#include <dlfcn.h>void *dlopen(const char *filename, int flag);void *dlsym(void *handle, const char *symbol);int dlclose(void *handle);char *dlerror(void); |
下面, 分别解释这几个函数
dlopen
dlopen()函数以指定模式打开指定的动态链接库文件,并返回动态链接库的句柄。参数flag有以下两种常用的值,并且必须指定其一。 RTLD_LAZY:在dlopen返回前,对于动态库中存在的未定义的变量(如外部变量extern,也可以是函数)不执行解析,就是不解析这个变量的地址。 RTLD_NOW:与上面不同,他需要在dlopen返回前,解析出每个未定义变量的地址,如果解析不出来,在dlopen会返回NULL.
dlsym
dlsym()函数根据动态链接库操作句柄(handle)与符号(symbol),返回符号对应的地址。使用这个函数不但可以获取函数地址,也可以获取变量地址。参数的含义如下: handle:由dlopen打开动态链接库后返回的指针; symbol:要求获取的函数或全局变量的名称。
dlclose
dlclose()函数用于关闭指定句柄的动态链接库,只有当此动态链接库的使用计数为0时,才会真正被系统卸载。
dlerror
当动态链接库操作函数,如dlopen/dlsym/dlclose//执行失败时,dlerror()函数可以返回最近的出错信息,返回值为NULL时表示操作函数执行成功。
C语言用户需要包含头文件dlfcn.h才能使用上述API。
然后, 我们新建一个main.c文件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/**#include <dlfcn.h>void *dlopen(const char *filename, int flag);void *dlsym(void *handle, const char *symbol);int dlclose(void *handle);char *dlerror(void);**/#include <stdio.h>#include <stdlib.h>#include <dlfcn.h>int main() { void *foo = dlopen("./a.so", RTLD_LAZY); dlerror(); void (*fun)() = dlsym(foo, "show"); int (*fun1)(int, int) = dlsym(foo, "add"); fun(); printf("%d\n", fun1(1, 2)); dlclose(foo); return 0;} |
然后编译命令
|
1
|
gcc main.c -ldl |
输出结果:
|
1
2
|
hello, world!3 |
C 语言 和 python 调用 .so 文件的更多相关文章
- Python 调用JS文件中的函数
Python 调用JS文件中的函数 1.安装PyExecJS第三方库 2.导入库:import execjs 3.调用JS文件中的方法 Passwd = execjs.compile(open(r&q ...
- windows下python调用c文件流程
1.新建fun.c文件和fun.h文件 #include <stdio.h> #include <stdlib.h> #include <string.h> int ...
- linux下python调用.so文件
前言 使用python 调用Fanuc的动态链路库.so 文件读取数据 环境要求 环境 需求 ubuntu16.04 32位 python3.5 32位 配置 把so文件添加到默认路径 ln -s / ...
- python调用其他文件的类和函数
在同一个文件夹下 调用函数 source.py文件: def func(): pass new.py文件: import source # 或者 from source import func 调用类 ...
- [C 语言基础] 如何调用不同文件中的函数
很多时候需要将实现不同功能的函数或者与某个模块有关的函数写在一个文件里.这样有两个好处: 1. 方便以后调用:以后需要用到这个模块,或者这类函数,直接将相关文件复制过去,再稍微修改一下就能应用于不同场 ...
- 【转】Python调用C语言动态链接库
转自:https://www.cnblogs.com/fariver/p/6573112.html 动态链接库在Windows中为.dll文件,在linux中为.so文件.以linux平台为例说明py ...
- python调用.so
python调用动态链接库的基本过程 动态链接库在Windows中为.dll文件,在linux中为.so文件.以linux平台为例说明python调用.so文件的使用方法. 本例中默认读者已经掌握动态 ...
- Python调用C模块以及性能分析
一.c,ctypes和python的数据类型的对应关系 ctypes type ctype Python type c_char char 1-character string c_wchar wch ...
- python 调用c语言函数
虽然python是万能的,但是对于某些特殊功能,需要c语言才能完成.这样,就需要用python来调用c的代码了 具体流程: c编写相关函数 ,编译成库 然后在python中加载这些库,指定调用函数. ...
随机推荐
- VMWare workstation Pro 14 For Linux key
VMWare workstation Pro 14 For Linux key: (我使用的Linux 系统是 Ubuntu16.04, 64位 ) 镜像是官方网址下载的,你也可以自己去官方网址下载: ...
- BFS:HDU2612-Find a way(双向BFS)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- ACM模板
#include <iostream> //万能头文件#include<bits/stdc++.h> 方便时用 #include <algorithm> #incl ...
- 1010: [HNOI2008]玩具装箱toy(斜率优化)
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 12280 Solved: 5277[Submit][S ...
- XX公司在线笔试题编程题之一
题目: #include <iostream> #include <vector> #include <string> #include <list> ...
- 使用 Dom4j 将 XML 转换为 MAP
本文为转载:http://blog.sina.com.cn/s/blog_6145ed810100z164.html 原文地址. 自己仅作备忘录方便查找留了一份. 这是解析Xml 的辅助类 pack ...
- laravel5.2总结--任务调度
你可以通过 command() 来调用 artisan 命令, call 来调用方法或函数, 或者 terminal() 来执行单行命令脚本: 1.在app/Console/Commands文件夹 ...
- 【Gas Station】cpp
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- 【Search In Rotated Sorted Array】cpp
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- 为什么对多线程编程这么怕?pthread,sem,mutex,process
转自http://blog.chinaunix.net/uid-20788636-id-1841334.html 1.线程创建和退出创建线程实际上就是确定调用该线程函数的入口点,这里通常使用的函数是p ...