C++ dlopen使用 Loading Functions

Loading Functions

In C++ functions are loaded just like in C, with dlsym. The functions you want to load must be qualified as extern “C” to avoid the symbol name being mangled.

Example 1. Loading a Function

main.cpp:

#include
#include <dlfcn.h>

int main() {
using std::cout;
using std::cerr;

cout << "C++ dlopen demo\n\n";// open the library
cout << "Opening hello.so...\n";
void* handle = dlopen("./hello.so", RTLD_LAZY);if (!handle) {cerr << "Cannot open library: " << dlerror() << '\n';return 1;
}// load the symbol
cout << "Loading symbol hello...\n";
typedef void (*hello_t)();
hello_t hello = (hello_t) dlsym(handle, "hello");
if (!hello) {cerr << "Cannot load symbol 'hello': " << dlerror() <<'\n';dlclose(handle);return 1;
}// use it to do the calculation
cout << "Calling hello...\n";
hello();// close the library
cout << "Closing library...\n";
dlclose(handle);

}

hello.cpp:

#include

extern “C” void hello() {
std::cout << “hello” << ‘\n’;
}
链接class;
main.cpp:

#include “polygon.hpp”
#include
#include <dlfcn.h>

int main() {
using std::cout;
using std::cerr;

// load the triangle library
void* triangle = dlopen("./triangle.so", RTLD_LAZY);
if (!triangle) {cerr << "Cannot load library: " << dlerror() << '\n';return 1;
}// load the symbols
create_t* create_triangle = (create_t*) dlsym(triangle, "create");
destroy_t* destroy_triangle = (destroy_t*) dlsym(triangle, "destroy");
if (!create_triangle || !destroy_triangle) {cerr << "Cannot load symbols: " << dlerror() << '\n';return 1;
}// create an instance of the class
polygon* poly = create_triangle();// use the class
poly->set_side_length(7);cout << "The area is: " << poly->area() << '\n';// destroy the class
destroy_triangle(poly);// unload the triangle library
dlclose(triangle);

}

polygon.hpp:

#ifndef POLYGON_HPP
#define POLYGON_HPP

class polygon {
protected:
double side_length_;

public:
polygon()
: side_length_(0) {}

void set_side_length(double side_length) {side_length_ = side_length;
}virtual double area() const = 0;

};

// the types of the class factories
typedef polygon* create_t();
typedef void destroy_t(polygon*);

#endif

triangle.cpp:

#include “polygon.hpp”
#include

class triangle : public polygon {
public:
virtual double area() const {
return side_length_ * side_length_ * sqrt(3) / 2;
}
};

// the class factories

extern “C” polygon* create() {
return new triangle;
}

extern “C” void destroy(polygon* p) {
delete p;
}

There are a few things to note when loading classes:

You must provide both a creation and a destruction function; you must not destroy the instances using delete from inside the executable, but always pass it back to the module. This is due to the fact that in C++ the operators new and delete may be overloaded; this would cause a non-matching new and delete to be called, which could cause anything from nothing to memory leaks and segmentation faults. The same is true if different standard libraries are used to link the module and the executable.The destructor of the interface class should be virtual in any case. There might be very rare cases where that would not be necessary, but it is not worth the risk, because the additional overhead can generally be ignored.If your base class needs no destructor, define an empty (and virtual) one anyway; otherwise you will have problems sooner or later; I can guarantee you that. You can read more about this problem in the comp.lang.c++ FAQ at http://www.parashift.com/c++-faq-lite/, in section 20.

链接:
http://www.faqs.org/docs/Linux-mini/C+±dlopen.html

C++ dlopen使用相关推荐

  1. System.err: java.lang.UnsatisfiedLinkError: dlopen failed: library “libc++_shared.so“ not found

    Android Studio 配置OpenCV 的时候出现这样的提示 黄色警告libc++_shared.so" not found : System.err: java.lang.Unsa ...

  2. c语言 获取文件名的相对路径,c – 如何获取对应于给予dlopen的相对路径的绝对库文件名?...

    在我的程序中,我有如下代码 /* libname may be a relative path */ void loadLib(char const *libname) { void *handle ...

  3. 分析Android :java.lang.UnsatisfiedLinkError: dlopen failed * is 32-bit instead of 64-bit

    Crash 日志: java.lang.UnsatisfiedLinkError: dlopen failed: "/data/data/com.ireader.plug.sdk/iread ...

  4. 关于android 5.0报错:dlopen failed: couldn't map ... Permission denied

    问题描述: 我的应用当中集成了一个安全相关的sdk,而这个sdk中使用的so是加过壳的. 它加载native so的方式是:Java System.loadLibrary --> native ...

  5. dlopen failed: library “libopencv_java4.so“ not found 解决

    dlopen failed: library "libopencv_java4.so" not found Android studio编译yolov5时报错, 最后解决方法: a ...

  6. 用g++编译生成动态连接库*.so的方法及连接(dlopen() dlsym() dlclose())

    ================================================================ //test_so_file.h ifndef TEST_SO_FIL ...

  7. 【Android 逆向】Android 进程注入工具开发 ( 注入代码分析 | 获取 linker 中的 dlopen 函数地址 并 通过 远程调用 执行该函数 )

    文章目录 一.dlopen 函数简介 二.获取 目标进程 linker 中的 dlopen 函数地址 三.远程调用 目标进程 linker 中的 dlopen 函数 一.dlopen 函数简介 dlo ...

  8. 【Android 逆向】Android 进程注入工具开发 ( EIP 寄存器指向 dlopen 函数 | ESP 寄存器指向栈内存 | 调试程序收回目标进程控制权 )

    文章目录 一.EIP 寄存器指向 dlopen 函数 二.ESP 寄存器指向栈内存 三.调试程序收回目标进程控制权 一.EIP 寄存器指向 dlopen 函数 代码段中 , 一般都有 dlopen 函 ...

  9. 【错误记录】NDK 动态库报错 ( dlopen failed: file offset for the library /lib/arm64/libwebp.so“ >= file size:0)

    文章目录 一.报错信息 二.解决方案 一.报错信息 运行 NDK 时 , 删除了引用的动态库 , 然后 " Ctrl + Z " 恢复 , 运行就报如下错误 ; 2021-04-2 ...

  10. 采用dlopen、dlsym、dlclose dlopen dlerror加载动态链接库【总结】

    1.前言为了使程序方便扩展,具备通用性,可以采用插件形式.采用异步事件驱动模型,保证主程序逻辑不变,将各个业务已动态链接库的形式加载进来,这就是所谓的插件.linux提供了加载和处理动态链接库的系统调 ...

最新文章

  1. AngularJS2.0 quick start——其和typescript结合需要额外依赖
  2. 5.6 图像颜色映射
  3. 曹大:我的快速阅读法
  4. 第一章 Spark系统概述
  5. 在编程竞赛中,有6个评委为参赛的选手打分,分数为0-100的整数分。 * (静态初始化一个数组,在数组中随意写入6个分数) 选手的最后得分为: * 去掉一个最高分和一个最低分后 的4个评委平均值。
  6. MySQL8.0版本和5.7通过Navicat远程连接
  7. 外设驱动库开发笔记29:DS17887实时时钟驱动
  8. [转载] [python3教程]第七章.输入输出(Input and Output)
  9. leetcode 65. Valid Number
  10. Backup Exec 在Windows平台下安装、设置及对Oracle数据库备份详细说明
  11. 高新技术企业都需要准备哪些资料
  12. 杰瑞·卡普兰:人工智能并不可怕 未来将带来两大影响
  13. Convex Clustering(凸聚类)
  14. 网易公开课付费视频没有加密,可以随意下载到本地
  15. Android MVP 架构设计 (一)
  16. matlab怎么取消科学计数法_在MATLAB中更改数据科学计数法
  17. 字符串的子串计算方法
  18. 天池小布助手对话短文本语义匹配-文本二分类实践(pytorch)
  19. 开眼角会不会留疤,开眼角术后疤痕增生怎么办
  20. 美团饿了么外卖CPS项目怎么做?简单推广每天躺赚(附源码和搭建教程)

热门文章

  1. C语言:自增运算符和自减运算符
  2. 纯干货!Linux网络内核探究
  3. NLP领域中的预训练模型杂谈
  4. 自制一个输入网址就能打开网站的程序
  5. js 单引号替换成双引号,双引号替换成单引号 操作
  6. python+nodejs+vue社区志愿者活动报名服务管理系统源码
  7. 询问new bing关于android开发的15个问题(前景、未来、发展方向)
  8. 使用Zuul构建微服务网关(红莲业火)
  9. Matlab/Simulink 自动代码生成详细步骤
  10. BTC-加密哈希函数