C语言加载动态库

头文件:#include<dlfcn.h>

void * dlopen(const char* pathName, int mode); 返回值 handle

void *dlsym(void *handle, const char* symbol); 返回值 函数起始地址

handle是使用dlopen函数之后返回的句柄,symbol是要求获取的函数的名称,函数,返回值是void*,指向函数的地址;

测试:

创建一个.c文件,编译成动态链接库

/*************************************************************************
> File Name: hello.c
> Author:
> Mail:
> Created Time: 2019年12月12日 星期四 14时39分42秒
************************************************************************/ #include<stdio.h> int hello() {
printf("hello c \n");
return ;
}
gcc -shared -fPIC hello.c -o hello.so

创建一个头文件申明hello函数

/*************************************************************************
> File Name: hello.h
> Author:
> Mail:
> Created Time: 2019年12月12日 星期四 14时40分25秒
************************************************************************/ #ifndef _HELLO_H
#define _HELLO_H int hello(); #endif

创建一个main.c用于测试编译的hello.so是否正确

/*************************************************************************
> File Name: mian.c
> Author:
> Mail:
> Created Time: 2019年12月12日 星期四 14时41分22秒
************************************************************************/ #include<stdio.h>
#include"hello.h" int main() {
hello();
return ;
}

使用动态链接库编译

gcc main.c -o app ./hello.so

运行编译好的app文件输出如下

hello c

证明动态链接库编译成功,接下来使用dlopen和dlsym调用hello.so内的hello函数

/*************************************************************************
> File Name: mian.c
> Author:
> Mail:
> Created Time: 2019年12月12日 星期四 14时41分22秒
************************************************************************/ #include<stdio.h>
#include<dlfcn.h>
#include"hello.h" typedef int (*callback)();
static callback cb; int main() {
void* handle;
handle = dlopen("./hello.so",RTLD_LAZY);
if (!handle) {
printf("handle is null \n");
return ;
}
cb = (callback)dlsym(handle, "hello");
cb();
return ;
}

编译main.c 需要链接静态库

gcc main.c -o app -ldl

运行程序

hello c

可以发现与之前使用动态链接库编译运行结果相同。

C语言 dlopen dlsym的更多相关文章

  1. dlopen, dlsym今天才刚知道干什么用的,羞死人了

    dlopen, dlsym今天才刚知道干什么用的,羞死人了

  2. LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror(转)

    dlopen 基本定义 功能:打开一个动态链接库  包含头文件:  #include <dlfcn.h>  函数定义:  void * dlopen( const char * pathn ...

  3. LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror

    本定义 功能:打开一个动态链接库 包含头文件: #include <dlfcn.h> 函数定义: void * dlopen( const char * pathname, int mod ...

  4. 加载动态链接库——dlopen dlsym dlclose

    DLOPEN DLMOPEN DLCLOSE NAME     dlclose, dlopen, dlmopen - 打开/关闭共享对象 SYNOPSIS #include <dlfcn.h&g ...

  5. C - dlopen dlsym

    -----------------------------------------------------------------------------dlsym------------------ ...

  6. C语言动态调用库(转)

    转自:http://cloverprince.iteye.com/blog/481309 现有一个主程序用C语言写成.现在要允许第三方开发人员编写扩展的模块,约定第三方开发的模块必须提供一系列已知名称 ...

  7. C 语言 和 python 调用 .so 文件

    什么是静态库和动态库, 看一篇博客 http://www.cnblogs.com/skynet/p/3372855.html 现在,我们首先生成.so文件 首先, 我们写一个a.c文件 1 2 3 4 ...

  8. 执行dlsym()函数出现: undefined symbol

    执行dlsym()函数出现: undefined symbol 执行dlsym()函数出现: undefined symbol 当这个问题出现的时候,可以检查产生so文件的cpp文件,看看是否已经用  ...

  9. 精通UNIX下C语言编程与项目实践

    cc  -I  //include 目录 -L //静态库目录?动态也可以 -l //小写L,接静态库名称?动态也可以 -DXXX='"XXFF"' //-D直接定义宏 -c 只编 ...

随机推荐

  1. 触发bfc解决父子元素嵌套垂直方向margin塌陷问题

    首先看一下问题案例 .wrapper{             width: 100px;             height: 100px;             background-colo ...

  2. git中ignore文件配置

    在项目中我们有一些文件是不能公开的,或者说是每个人需要单独配置的,那么这个时候使用 git 就不能上传这些文件.此时就需要对 .gitignore 文件进行配置. git 的忽略原则:参考 廖雪峰的g ...

  3. 每日一问:LayoutParams 你知道多少?

    前面的文章中着重讲解了 View 的测量流程.其中我提到了一句非常重要的话:View 的测量匡高是由父控件的 MeasureSpec 和 View 自身的 `LayoutParams 共同决定的.我们 ...

  4. keepass口令管理实践

    语言修改 加入插件 插件的学习及应用

  5. 实现简单的AOP前置后置增强

    AOP操作是我们日常开发经常使用到的操作,例如都会用到的spring事务管理.今天我们通过一个demo实现对一个类的某一个方法进行前置和后置的增强. //被增强类 public class PetSt ...

  6. linux_权限小练习

    下面是解释: 当前# root用户, ls列出列表 mkdir 新建目录 赋予目录744权限      对others来说是  r--  只可读 touch 新建在目录下的同名文件 赋予文件600权限 ...

  7. idea git使用记录

    ps:记录idea中git的使用过程 在IntelliJ IDEA使用.gitignore插件的方法和作用 在IntelliJ IDEA使用.gitignore插件的方法和作用 idea git st ...

  8. Java8 新特性 Lamdba表达式

    Lamdba 表达式为什么出现   Java8是自java延生以来最大的改变,他允许java中的方法和函数成为一等公民(可以在方法间传递),所以就应运而出现了Lamdba表达式,他可以将表达式传递给另 ...

  9. python实现双向循环链表

    参考https://www.cnblogs.com/symkmk123/p/9693872.html#4080149 # -*- coding:utf-8 -*- # __author__ :kusy ...

  10. 配置opencv cmake

    第一种使用 find_package的方法示例代码如下:# 声明要求的 cmake 最低版本cmake_minimum_required( VERSION 2.8 ) # 声明一个 cmake 工程p ...