什么是静态库和动态库, 看一篇博客

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 cdll
 
cur = cdll.LoadLibrary('./a.so')
 
cur.show()
 
print cur.add(12)

 结果如下

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)(intint) = 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 文件的更多相关文章

  1. Python 调用JS文件中的函数

    Python 调用JS文件中的函数 1.安装PyExecJS第三方库 2.导入库:import execjs 3.调用JS文件中的方法 Passwd = execjs.compile(open(r&q ...

  2. windows下python调用c文件流程

    1.新建fun.c文件和fun.h文件 #include <stdio.h> #include <stdlib.h> #include <string.h> int ...

  3. linux下python调用.so文件

    前言 使用python 调用Fanuc的动态链路库.so 文件读取数据 环境要求 环境 需求 ubuntu16.04 32位 python3.5 32位 配置 把so文件添加到默认路径 ln -s / ...

  4. python调用其他文件的类和函数

    在同一个文件夹下 调用函数 source.py文件: def func(): pass new.py文件: import source # 或者 from source import func 调用类 ...

  5. [C 语言基础] 如何调用不同文件中的函数

    很多时候需要将实现不同功能的函数或者与某个模块有关的函数写在一个文件里.这样有两个好处: 1. 方便以后调用:以后需要用到这个模块,或者这类函数,直接将相关文件复制过去,再稍微修改一下就能应用于不同场 ...

  6. 【转】Python调用C语言动态链接库

    转自:https://www.cnblogs.com/fariver/p/6573112.html 动态链接库在Windows中为.dll文件,在linux中为.so文件.以linux平台为例说明py ...

  7. python调用.so

    python调用动态链接库的基本过程 动态链接库在Windows中为.dll文件,在linux中为.so文件.以linux平台为例说明python调用.so文件的使用方法. 本例中默认读者已经掌握动态 ...

  8. Python调用C模块以及性能分析

    一.c,ctypes和python的数据类型的对应关系 ctypes type ctype Python type c_char char 1-character string c_wchar wch ...

  9. python 调用c语言函数

    虽然python是万能的,但是对于某些特殊功能,需要c语言才能完成.这样,就需要用python来调用c的代码了 具体流程: c编写相关函数 ,编译成库 然后在python中加载这些库,指定调用函数. ...

随机推荐

  1. 用PHP和Python生成短链接服务的字符串ID

    假设你想做一个像微博短链接那样的短链接服务,短链接服务生成的URL都非常短例如: http://t.cn/E70Piib, 我们应该都能想到链接中的E70Piib对应的就是存储长链接地址的数据记录的I ...

  2. IE支持直接查看Json数据注册表代码

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/json] ...

  3. requests.exceptions.SSLError……Max retries exceeded with url错误求助!!!

    import requests head = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Appl ...

  4. beautifulsoup解析

    beautifulsoup解析 python独有 优势:简单.便捷.高效 - 环境安装 需要将pip源设置为国内源 -需要安装:pip install bs4 bs4在使用时需要一个第三方库 pip ...

  5. A1083 List Grades (25)(25 分)

    A1083 List Grades (25)(25 分) Given a list of N student records with name, ID and grade. You are supp ...

  6. Kali 网络配置

    一.配置IP 编辑/etc/network/interfaces # This file describes the network interfaces available on your syst ...

  7. MVC中Spring.net 对基类控制器无效 过滤器控制器无效

    比如现在我又一个BaseController作为基类控制器,用于过滤权限.登录判断等作用,其它控制由原本的继承Controller,改为继承BaseController.然后BaseControlle ...

  8. MVC6学习教程

    http://www.cnblogs.com/TomXu/p/4495251.html

  9. linux环境搭建系列之svn安装

    前提: linux centOS 64位操作系统 1.root账号 2.#yum install -y subversion 出现如下报错: 尝试Telnet https://www.baidu.co ...

  10. 使用Fiddler对Android应用进行抓包

    1.  打开Fiddler软件,效果图如下: 2. 首先,确保安装 Fiddler 的电脑和你的手机在同一局域网内,因为Fiddler只是一个代理,需要将手机的代理指向 PC 机,不能互相访问是不行的 ...