注意其中使用函数返回基类指针的用法,因为Linux的动态链接库不能像MFC中那样直接导出类

一、介绍


如何使用dlopen API动态地加载C++函数和类,是Unix C++程序员经常碰到的问题。


事实上,情况偶尔有些复杂,需要一些解释。这正是写这篇mini HOWTO的缘由。

理解这篇文档的前提是对C/C++语言中dlopen API有基本的了解。


这篇HOWTO的维护链接是:


http://www.isotton.com/howtos/C++-dlopen-mini-HOWTO/

二、问题所在


有时你想在运行时加载一个库(并使用其中的函数),这在你为你的程序写一些插件或模块架构的时候经常发生。


在C语言中,加载一个库轻而易举(调用dlopen、dlsym和dlclose就够了),但对C++来说,情况稍微复杂。

动态加载一个C++库的困难一部分是因为C++的name mangling


(译者注:也有人把它翻译为“名字毁坏”,我觉得还是不翻译好),


另一部分是因为dlopen API是用C语言实现的,因而没有提供一个合适的方式来装载类。

在解释如何装载C++库之前,最好再详细了解一下name mangling。


我推荐您了解一下它,即使您对它不感兴趣。因为这有助于您理解问题是如何产生的,如何才能解决它们。

1. Name Mangling


在每个C++程序(或库、目标文件)中,


所有非静态(non-static)函数在二进制文件中都是以“符号(symbol)”形式出现的。


这些符号都是唯一的字符串,从而把各个函数在程序、库、目标文件中区分开来。

在C中,符号名正是函数名:strcpy函数的符号名就是“strcpy”,等等。


这可能是因为两个非静态函数的名字一定各不相同的缘故。

而C++允许重载(不同的函数有相同的名字但不同的参数),


并且有很多C所没有的特性──比如类、成员函数、异常说明──几乎不可能直接用函数名作符号名。

为了解决这个问题,C++采用了所谓的name mangling。它把函数名和一些信息(如参数数量和大小)杂糅在一起,


改造成奇形怪状,只有编译器才懂的符号名。

例如,被mangle后的foo可能看起来像foo@4%6^,或者,符号名里头甚至不包括“foo”。

其中一个问题是,C++标准(目前是[ISO14882])并没有定义名字必须如何被mangle,


所以每个编译器都按自己的方式来进行name mangling。


有些编译器甚至在不同版本间更换mangling算法(尤其是g++ 2.x和3.x)。


即使您搞清楚了您的编译器到底怎么进行mangling的,从而可以用dlsym调用函数了,


但可能仅仅限于您手头的这个编译器而已,而无法在下一版编译器下工作。

三、类


使用dlopen API的另一个问题是,它只支持加载函数。


但在C++中,您可能要用到库中的一个类,而这需要创建该类的一个实例,这不容易做到。

四、解决方案


1. extern "C"


C++有个特定的关键字用来声明采用C binding的函数:


  extern "C" 。

用 extern "C"声明的函数将使用函数名作符号名,就像C函数一样。


因此,只有非成员函数才能被声明为extern "C",并且不能被重载。

尽管限制多多,extern "C"函数还是非常有用,因为它们可以象C函数一样被dlopen动态加载。


冠以extern "C"限定符后,并不意味着函数中无法使用C++代码了,


相反,它仍然是一个完全的C++函数,可以使用任何C++特性和各种类型的参数。

2. 加载函数


在C++中,函数用dlsym加载,就像C中一样。不过,该函数要用extern "C"限定符声明以防止其符号名被mangle。

示例1.加载函数


代码:


//----------


//main.cpp:


//----------


#include <iostream>


#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)();

// reset errors


  dlerror();


  hello_t hello = (hello_t) dlsym(handle, "hello");


  const char *dlsym_error = dlerror();


  if (dlsym_error) {


    cerr << "Cannot load symbol 'hello': " << dlsym_error <<'\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 <iostream>

extern "C" void hello() {


  std::cout << "hello" << '\n';


}

在hello.cpp中函数hello被定义为extern "C"。它在main.cpp中被dlsym调用。


函数必须以extern "C"限定,否则我们无从知晓其符号名。

警告:


extern "C"的声明形式有两种:


  上面示例中使用的那种内联(inline)形式extern "C" , 


  还有只用花括号的extern "C" { ... }这种。

第一种内联形式声明包含两层意义:外部链接(extern linkage)和C语言链接(language linkage),


而第二种仅影响语言链接。

下面两种声明形式等价:


代码:


extern "C" int foo;


extern "C" void bar();

和代码:


extern "C" {


  extern int foo;


  extern void bar();


}

对于函数来说,extern和non-extern的函数声明没有区别,但对于变量就有不同了。

如果您声明变量,请牢记:


代码:


extern "C" int foo;

和代码:


extern "C" {


  int foo;


}

是不同的物事(译者注:简言之,前者是个声明; 而后者不仅是声明,也可以是定义)。


进一步的解释请参考[ISO14882],7.5, 特别注意第7段;


 或者参考[STR2000],9.2.4。


在用extern的变量寻幽访胜之前,请细读“其他”一节中罗列的文档。

3. 加载类


加载类有点困难,因为我们需要类的一个实例,而不仅仅是一个函数指针。


我们无法通过new来创建类的实例,因为类不是在可执行文件中定义的,况且(有时候)我们连它的名字都不知道。

解决方案是:利用多态性!

我们在可执行文件中定义一个带虚成员函数的接口基类,而在模块中定义派生实现类。


通常来说,接口类是抽象的(如果一个类含有虚函数,那它就是抽象的)。


因为动态加载类往往用于实现插件,


这意味着必须提供一个清晰定义的接口──我们将定义一个接口类和派生实现类。

接下来,在模块中,我们会定义两个附加的helper函数,


就是众所周知的“类工厂函数(class factory functions)(译者注:或称对象工厂函数)”。

其中一个函数创建一个类实例,并返回其指针; 


另一个函数则用以销毁该指针。这两个函数都以extern "C"来限定修饰。

为了使用模块中的类,我们用dlsym像示例1中加载hello函数那样加载这两个函数,


然后我们就可以随心所欲地创建和销毁实例了。

示例2.加载类


我们用一个一般性的多边形类作为接口,而继承它的三角形类(译者注:正三角形类)作为实现。


代码:


//----------


//main.cpp:


//----------


#include "polygon.hpp"


#include <iostream>


#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;


  }

// reset errors


  dlerror();

// load the symbols


  create_t* create_triangle = (create_t*) dlsym(triangle, "create");


  const char* dlsym_error = dlerror();

if (dlsym_error) {


    cerr << "Cannot load symbol create: " << dlsym_error << '\n';


    return 1;


  }

destroy_t* destroy_triangle = (destroy_t*) dlsym(triangle, "destroy");


  dlsym_error = dlerror();


  if (dlsym_error) {


    cerr << "Cannot load symbol destroy: " << dlsym_error << '\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);


}

主程序的编译与运行:


$ g++ -Wall -g -rdynamic -ldl main.cpp -o compile_c++LIBc++


$ ./compile_c++LIBc++ 


The area is: 42.4352

//----------


//polygon.hpp:


//----------


#ifndef POLYGON_HPP


#define POLYGON_HPP

class polygon {


  protected:


    double side_length_;

public:


    polygon(): side_length_(0) {}

virtual ~polygon() {}

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 <cmath>

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;


}


动态库的编译:


$ g++ -Wall -g -fPIC -o triangle.so -shared triangle.cpp

加载类时有一些值得注意的地方:


◆ 你必须(译者注:在模块或者说共享库中)同时提供一个创造函数和一个销毁函数,


   且不能在执行文件内部使用delete来销毁实例,只能把实例指针传递给模块的销毁函数处理。


   这是因为C++里头,new操作符可以被重载;


   这容易导致new-delete的不匹配调用,造成莫名其妙的内存泄漏和段错误。


   这在用不同的标准库链接模块和可执行文件时也一样。


◆ 接口类的析构函数在任何情况下都必须是虚函数(virtual)。


   因为即使出错的可能极小,近乎杞人忧天了,但仍旧不值得去冒险,反正额外的开销微不足道。


   如果基类不需要析构函数,定义一个空的(但必须虚的)析构函数吧,否则你迟早要遇到问题,我向您保证。


   你可以在comp.lang.c++ FAQ( http://www.parashift.com/c++-faq-lite/ )的


   第20节了解到更多关于该问题的信息。

示例3:


/*!


 ******************************************************************************


 * \File


 *   arith.h


 ******************************************************************************


 */ 


#ifndef __ARITH_H__


#define __ARITH_H__


    


class Arithmetic





  protected:


    int m_iVarA;


    int m_iVarB;


    


  public:


    void set_member_var(int a, int b){


      m_iVarA = a;


      m_iVarB = b;


    }


    


  public:


    virtual int add() const = 0;


    //int add();


    int sub();


    int mul();


    int div();


    int mod();

public:


    Arithmetic():m_iVarA(0),m_iVarB(0){}


    virtual ~Arithmetic(){}


};

typedef Arithmetic* create_t();


typedef void destroy_t(Arithmetic*);


#endif

/*!


 ******************************************************************************


 * \File


 *   arith.cpp


 ******************************************************************************


 */ 


#include "arith.h"

class arith : public Arithmetic{


  public:


    virtual int add() const {


      return (m_iVarA + m_iVarB);


    }


};  


    


// the class factories


extern "C" Arithmetic* create(int a, int b) {


  return new arith;


}     


    


extern "C" void destroy(Arithmetic* p) {


  delete p;


}

编译动态库:


$ g++ -Wall -g -fPIC -o arith.so -shared arith.cpp

主程序:


/*!


 ******************************************************************************


 * \File


 *   main.cpp


 * \Brief


 *   C++ source code


 * \Author


 *   Hank


 ******************************************************************************


 */


#include <iostream>


#include <dlfcn.h>

#include "arith.h"

using namespace std;

int main(int argc, char* argv[])


{


  int a = 4, b = 3;


  int ret = 0;

void *p_Handler = dlopen("./arith.so", RTLD_LAZY);


  if (!p_Handler)


  {


    printf("%s\n",dlerror());


    exit(1);


  }

dlerror();

create_t* create_arith = (create_t*)dlsym(p_Handler, "create");


  const char* dlsym_error = dlerror();


  if (dlsym_error) {


    cerr << "Cannot load symbol create: " << dlsym_error << '\n';


    return 1;


  }

destroy_t* destroy_arith = (destroy_t*)dlsym(p_Handler, "destroy");


  dlsym_error = dlerror();


  if (dlsym_error) {


    cerr << "Cannot load symbol destroy: " << dlsym_error << '\n';


    return 1;


  }

Arithmetic* arith_obj = create_arith();

arith_obj->set_member_var(a, b);

ret = arith_obj->add();

cout<<a<<" + "<<b<<" = "<<ret<<endl;

destroy_arith(arith_obj);

dlclose(p_Handler);


  return 0;


}

编译与运行:


$ g++ -Wall -g -rdynamic -ldl main.cpp -o compile_c++LIBc++


$ ./compile_c++LIBc++ 


4 + 3 = 7

 

linux下C++动态链接C++库示例详解的更多相关文章

  1. Linux下nginx编译安装教程和编译参数详解

    这篇文章主要介绍了Linux下nginx编译安装教程和编译参数详解,需要的朋友可以参考下 一.必要软件准备1.安装pcre 为了支持rewrite功能,我们需要安装pcre 复制代码代码如下: # y ...

  2. Linux下的I/O复用与epoll详解(转载)

    Linux下的I/O复用与epoll详解 转载自:https://www.cnblogs.com/lojunren/p/3856290.html  前言 I/O多路复用有很多种实现.在linux上,2 ...

  3. linux下 GCC编译链接静态库&动态库

    静态库 有时候需要把一组代码编译成一个库,这个库在很多项目中都要用到,例如libc就是这样一个库, 我们在不同的程序中都会用到libc中的库函数(例如printf),也会用到libc中的变量(例如以后 ...

  4. Linux下双网卡绑定bond配置实例详解

    本文源自:http://blog.itpub.net/31015730/viewspace-2150185/ 一.什么是bond? 网卡bond是通过多张网卡绑定为一个逻辑网卡,实现本地网卡冗余,带宽 ...

  5. Linux下的I/O复用与epoll详解

    前言 I/O多路复用有很多种实现.在linux上,2.4内核前主要是select和poll,自Linux 2.6内核正式引入epoll以来,epoll已经成为了目前实现高性能网络服务器的必备技术.尽管 ...

  6. Linux下的压缩zip,解压缩unzip命令详解及实例

    实例:压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip ====================== ...

  7. Linux下添加硬盘,分区,格式化详解

    2005-10-17 在我们添加硬盘前,首先要了解linux系统下对硬盘和分区的命名方法. 在Linux下对IDE的设备是以hd命名的,第一个ide设备是hda,第二个是hdb.依此类推 我们一般主板 ...

  8. Linux下的crontab定时执行任务命令详解

    在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps -ef|grep cron].cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.cron的配置文件称为“cr ...

  9. Linux下配置Node环境变量及问题详解

    这是之前在Linux下配置Node环境变量时踩过的坑,今天又有小伙伴询问这个问题,因此记录下来,不仅是给新童鞋们一些参考,也方便日后查阅 在这之前,相信都已经安装好了,没安装的可以查看博主另一篇文章 ...

随机推荐

  1. Python 2.7 Exception格式化工具

    首先说一个发现: try: 抛错误,抛异常 except Exception as e: 都被这里抓住了 except Error as e: 这里啥事都没了 然后,说说Exception as e的 ...

  2. jquery无缝滚动效果实现

    demo如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  3. mysql基础之对库表操作

    原文:mysql基础之对库表操作 查看一下所有的库,怎么办? Mysql>Show databases; 选库语句: Use 库名 创建一个数据库: create database 数据库名 [ ...

  4. EF的四种开发模式

    EF提供了四种开发模式,具体如下:(转载)Code First(New DataBase) :在代码中定义类和映射关系并通过model生成数据库,使用迁移技术更新数据库.Code First(Exis ...

  5. wcf跨机器访问的问题

    wcf跨机器访问的问题 在wcf跨机器的访问中遇到了各种无法访问的问题,本人也是在通过个人解决问题的基础上发表一下自己的经验,如果还有其他方面可能影响wcf跨机器的问题,还希望大家多多发言! 好了废话 ...

  6. Web Api中实现Http方法(Put,Post,Delete)

    在Web Api中实现Http方法(Put,Post,Delete) 系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 在Web Api中,我 ...

  7. Linq无聊练习系列7----Insert,delete,update,attach操作练习

    /*********************Insert,delete,update,attach操作练习**********************************/            ...

  8. WPF开发的FTP文件上传工具

    前言 最近楼主在改版的项目里有使用到FTP上传文件,所以看到之前的项目里FTP上传的功能,然后抽个时间学习一番,做着做着就作出了这个工具了(虽然有些验证的功能没加上),但是基本的上传功能还是很好用的, ...

  9. Nginx+phpfastcgi下flush输出问题

    最近由于业务需要,需要使用php的flush输出缓存刷新,处理浏览器超时问题. 最初的测试代码如下: ob_start();//打开缓冲区 for ($i=10; $i>0; $i--) { e ...

  10. SQLite数据库操作 (原始操作)

    android提供了一个名为SQLiteDatabase的类,该类封装了一些操作数据库的API, 使用该类可以完成对数据进行添加(Create).查询(Retrieve).更新(Update)和删除( ...