前面我们在编译可执行文件时,如果可执行文件要依赖某个so。必须要通过-L指定so路径,并且-l指定so名字。

而且在可执行文件运行时,要先加载so的load部分到进程地址空间。

有一种方式可以在编译时不需要link so, 而且程序运行过程中去加载so。

dlopen函数可以在进程运行过程中,打开so,将其加载到进程的地址空间,并完成初始化过程。

如果dlopen中指定的路径是相对路径,那么按照LD_LIBRARY_PATH,/etc/ld.so.cache,/lib,/usr/lib顺序查找。否则直接打开so。

dlsym返回so的符号的值,如果符号是函数和变量,返回符号和变量的地址;如果符号是常量,就返回常量的值。

我们在前面写的消息队列msgsnd.c代码中稍作修改,以运行时加载libmsg.so。代码如下:

#include <sys/prctl.h>

#include <string.h>
#include <stdio.h>
#include <dlfcn.h>
#include "msg.h"
#define MSG_CREAT_PATH "/mnt/hgfs/share/test/list"
#define MSG_RCV_ID 4
#define MSG_SND_ID 3
typedef int (*create_msg_queue)(const char *path, int proj_id);
typedef int (*rcv_msg)(int id, FellowMsg *msg);
typedef int (*snd_msg)(int id, FellowMsg *msg);
typedef struct _MsgIf{
  create_msg_queue create;
  rcv_msg rcv;
  snd_msg snd;
}MsgIf;
MsgIf msgIf;
void *fellow_listenning_msg(void *arg)
{
  if(0 != prctl(PR_SET_NAME, (unsigned long)"fellow_process_msg"))
  {
    printf("prctl fail, errno:%d", errno);
  }
  int msg_q_id = msgIf.create(MSG_CREAT_PATH, MSG_RCV_ID);
  FellowMsg _fellowMsg;
  while (1)
  {
    memset(&_fellowMsg, 0, sizeof(FellowMsg));
    msgIf.rcv(msg_q_id, &_fellowMsg);
    if (CTRL_FEEDBACK ==_fellowMsg._msgType)
    {
      switch (_fellowMsg._ctlInfo.u._ctlFeedback)
      {
        case OPEN_DONE:
        printf("rcv OPEN_DONE:%d\n", _fellowMsg._ctlInfo.param);
        break;
        case CLOSE:
        printf("rcv CLOSE_DONE:%d\n", _fellowMsg._ctlInfo.param);
        break;
        case PLAY:
        printf("rcv PLAY_DONE:%d\n", _fellowMsg._ctlInfo.param);
        break;
        default:
        break;

      }
    }

  }
}
void main(void)
{
  void *handle = dlopen("/mnt/hgfs/share/test/msg/libmsg.so", RTLD_NOW);//打开libmsg.so
  if (NULL == handle)
  {
    printf("dlopen fail:%s\n", dlerror());
    return;
  }
  msgIf.create = (create_msg_queue)dlsym(handle, "fellow_create_msg_queue");//获取符号的值。
  msgIf.rcv = (rcv_msg)dlsym(handle, "fellow_rcv_msg");
  msgIf.snd = (snd_msg)dlsym(handle, "fellow_send_msg");
  pthread_t thread_id;
  int snd_msg_q_id = msgIf.create(MSG_CREAT_PATH, MSG_SND_ID);
  printf("msgid:%d\n",snd_msg_q_id);
  FellowMsg _fellowMsg;
  _fellowMsg._msgType = CTRL_CMD;
  _fellowMsg._ctlInfo.u._ctlCmd = OPEN;
  _fellowMsg._ctlInfo.param = 1;
  printf("create:%p, snd:%p, rcv:%p\n", msgIf.create, msgIf.snd,msgIf.rcv);
  msgIf.snd(snd_msg_q_id, &_fellowMsg);
  pthread_create(&thread_id, NULL, fellow_listenning_msg, NULL);
  while (1)
  {
  }
  dlclose(handle);
}

那么在编译时我们不需要link so: gcc msgsnd.c -o msgsnd -ldl -pthread

动态链接--运行时加载dlopen的更多相关文章

  1. Linux下显示运行时链接(运行时加载)

    目录 介绍 如何加载动态库 dlopen() 第一个参数: 被加载动态库的路径 第二个参数: flag表示函数符号的解析方式 dlopen 返回值 dlsym() 参数: 返回值 符号优先级 dler ...

  2. Windows 运行时加载动态库

    下面是一个运行时加载nvcuda.dll,并检测当前驱动版本最大支持的CUDA版本的例子. #include "cuda.h" #include <stdio.h> # ...

  3. QT运行时加载UI文件

      写QT程序里运行时加载UI文件,代码如下: 点击(此处)折叠或打开 #include "keyboard.h" #include <QtUiTools> #incl ...

  4. commonJs的运行时加载和es6的编译时加载

    参考 : https://www.cnblogs.com/jerrypig/p/8145206.html 1.commonJs的运行时加载 2.ES6编译时加载

  5. linux 运行时加载不上动态库 解决方法(转)

    1. 连接和运行时库文件搜索路径到设置     库文件在连接(静态库和共享库)和运行(仅限于使用共享库的程序)时被使用,其搜索路径是在系统中进行设置的.一般 Linux 系统把 /lib 和 /usr ...

  6. Spark on Yarn运行时加载的jar包

    spark on yarn运行时会加载的jar包有如下: spark-submit中指定的--jars $SPARK_HOME/jars下的jar包 yarn提供的jar包 spark-submit通 ...

  7. Java源文件编译成功但是运行时加载不到文件

    最近系统重装了一些,Java等环境变量都需要重新配置,配置好以后编写了一个Java源文件编译了一下,通过Javac编译源文件,编译成功,但是再通过Java运行时没找到报出找不到加载文件或者加载文件不存 ...

  8. spark运行时加载配置文件(hive,hdfs)

    文章为转载,如有版权问题,请联系,谢谢! 转自:https://blog.csdn.net/piduzi/article/details/81636253 适合场景:在运行时才确定用哪个数据源 imp ...

  9. 【转】Sqlite 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该...

    开发环境: vs2010+.net framework 4.0+ System.Data.SQLite.DLL (2.0)今天在做Sqlite数据库测试,一运行程序在一处方法调用时报出了一个异常 混合 ...

随机推荐

  1. dubbox生产者与消费者案例

    一.首先要将dubbox添加到本地maven仓库     参考: https://blog.csdn.net/try_and_do/article/details/83383861     二.目录结 ...

  2. mybatis一级缓存和二级缓存(三)

    缓存详细介绍,结果集展示 https://blog.csdn.net/u013036274/article/details/55815104   配置信息 http://www.pianshen.co ...

  3. 超简单的OpenGL & WebGL & Three.js介绍_1

    专业解释 什么是OpenGL OpenGL(Open Graphics Library即开放图形库或者“开放式图形库”)是用于渲染2D.3D矢量图形的跨语言.跨平台的应用程序编程接口(API). 这个 ...

  4. SUSE中搭建kafka

     搭建环境: JDK: java version 1.8.0_221 zookeeper:zookeeper-3.5.2 kafka: kafka-2.11-1.1.0 一.安装JDK 由于需要jav ...

  5. Flatmap 和map 区别

    map将函数作用到数据集的每一个元素上,生成一个新的分布式的数据集(RDD)返回 map函数的源码:   def map(self, f, preservesPartitioning=False): ...

  6. WDatePicker使用 出现ReferenceError: disabledDates is not defined

    "ReferenceError: disabledDates is not defined at eval (eval at <anonymous> at HTMLInputEl ...

  7. hashmap与currentHashMap

    hashmap的缺点 多线程不安全,在并发场景下使用时容易出现死循环,脏读问题等 死循环:https://juejin.im/post/5a66a08d5188253dc3321da0 (这篇好点)h ...

  8. HTML网站右键禁用F12代码 屏蔽审查元素 防止修改页面代码

    <script>//禁止右键 function click(e) { if (document.all) { if (event.button==2||event.button==3) { ...

  9. oneshot和周期性shot

    计数器的使用,oneshot:时刻. 有误差,日.每一些间隔可以产生周期性shot(多个持续性时刻)

  10. GitBook相关使用以及配置笔记

    本地安装 GitBook的安装非常简单.您的系统只需满足这两个要求: NodeJS(推荐使用v4.0.0及以上版本) Windows,Linux,Unix或Mac OS X gitbook-cli 是 ...