前面我们在编译可执行文件时,如果可执行文件要依赖某个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. Docker镜像加速-配置阿里云镜像仓库

    Docker默认远程仓库是https://hub.docker.com/ 比如我们下载一个大点的东西,龟速 由于是国外主机,类似Maven仓库,慢得一腿,经常延迟,破损: 所以我们一般都是配置国内镜像 ...

  2. Spring-JDBCTemplate介绍

        一.Spring对不同的持久化支持: Spring为各种支持的持久化技术,都提供了简单操作的模板和回调 ORM持久化技术 模板类 JDBC org.springframework.jdbc.c ...

  3. LightOJ-1370 Bi-shoe and Phi-shoe (欧拉函数+二分)

    Problem Description Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe ...

  4. 2.4测试赛AC代码临时保存

    //H #include<cstdio> #include<cstdlib> #include<cstring> #include<stack> usi ...

  5. c++指针实例

    #include <iostream> using namespace std; int main () { ; // 实际变量的声明 int* ip; // 指针变量的声明 ip = & ...

  6. 【算法学习记录-排序题】【PAT A1062】Talent and Virtue

    About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about ...

  7. 图解SOAPUI解析WSDL文件

    本文链接:https://blog.csdn.net/qq_16234613/article/details/53143279 新建项目 添加WSDL文件 查看方法 查看XML格式 运行测试  

  8. codeforces Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid

    #include<bits/stdc++.h> using namespace std ; int n; struct City { int id; long long x,y; //坐标 ...

  9. 题解 【Codeforces755A】 PolandBall and Hypothesis

    我们可以发现,当n>2时,n·(n-2)+1=(n-1)·(n-1),因此,输出n-2即可. 如果n<=2,我们可以发现: 当n=2时,2·4+1=9不是质数,输出4即可: 当n=1时,1 ...

  10. mysql学习笔记(1)

    以下笔记并不系统,只是针对遇到的问题和特别的点记录一下: 数据类型: 1.mysql小数存储数据类型 有float double decimal ,前两个不属于精确类型,不推荐使用,一般生产库亦不会使 ...