使用dlopen和dlsym来使用C++中的类

 使用dlopen和dlsym来使用C++中的类 2008-08-09 23:43:37php

分类:html

通常来讲,dlopen和dlsym是来处理C库中的函数的,对于C++中存在的name mangle问题,类的问题就不易处理,看下文你会有所收获。linux

转载自:ios

http://www.linuxsir.org/bbs/printthread.php?t=266890c++

 

C++ dlopen mini HOWTO 中译版 [原创]
 程序员

C++ dlopen mini HOWTO
做者:Aaron Isotton <aaron@isotton.com> 2006-03-16
译者:Lolita@linuxsir.org 2006-08-05
算法

------------------------------------------------
摘要
  如何使用dlopen API动态地加载C++函数和类

------------------------------------------------
目录
  介绍
    版权和许可证
    不承诺
    贡献者
    反馈
    术语
  问题所在
    Name Mangling
    类
  解决方案
    extern "C"
    加载函数
    加载类
  源代码 
  FAQ
  其余
  参考书

------------------------------------------------
介绍
  如何使用dlopen API动态地加载C++函数和类,是Unix C++程序员常常碰到的问题。事实上,状况偶尔有些复杂,须要一些解释。这正是写这篇mini HOWTO的原因。
  理解这篇文档的前提是对C/C++语言中dlopen API有基本的了解。这篇HOWTO的维护连接是 http://www.isotton.com/howtos/C++-dlopen-mini-HOWTO/ 

  版权和许可证
  这篇文档《C++ dlopen mini HOWTO》版权为Aaron Isotton全部(copyrighted (c) 2002-2006),任何人在遵照自由软件基金会制定的GPLv2许可证条款前提下能够自由拷贝、分发和修改这份文档。

  不承诺
  本文不对文中的任何内容做可靠性承诺。您必须为您本身使用文中任何概念、示例和信息承担风险,由于其中可能存在错误和不许确的地方,或许会损坏您的系统──尽管几乎不可能发生此类事故,但您仍是当心行事──做者不会为此负任何责任。

  贡献者
  在这篇文档中,我欣然致谢(按字母顺序):
  ◆ Joy Y Goodreau <joyg (at) us.ibm.com> 她的编辑工做.
  ◆ D. Stimitis <stimitis (at) idcomm.com> 指出一些formatting和name mangling的问题, 还指出extern “C”的一些微妙之处。

  反馈
  欢迎对本文档的反馈信息!请把您的补充、评论和批评发送到这个邮件地址:<aaron@isotton.com>。

  术语
  dlopen API
    关于dlclose、dlerror、dlopen和dlsym函数的描述能够在 dlopen(3) man手册页查到。
    请注意,咱们使用“dlopen”时,指的是dlopen函数,而使用“dlopen API”则是指整个API集合。
------------------------------------------------
问题所在
  有时你想在运行时加载一个库(并使用其中的函数),这在你为你的程序写一些插件或模块架构的时候常常发生。
  在C语言中,加载一个库垂手可得(调用dlopen、dlsym和dlclose就够了),但对C++来讲,状况稍微复杂。动态加载一个C++库的困难一部分是由于C++的name mangling(译者注:也有人把它翻译为“名字毁坏”,我以为仍是不翻译好),另外一部分是由于dlopen API是用C语言实现的,于是没有提供一个合适的方式来装载类。
  在解释如何装载C++库以前,最好再详细了解一下name mangling。我推荐您了解一下它,即便您对它不感兴趣。由于这有助于您理解问题是如何产生的,如何才能解决它们。

  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++中,您可能要用到库中的一个类,而这须要建立该类的一个实例,这不容易作到。

解决方案

  extern "C"
  C++有个特定的关键字用来声明采用C binding的函数:extern "C" 。 用 extern "C"声明的函数将使用函数名做符号名,就像C函数同样。所以,只有非成员函数才能被声明为extern "C",而且不能被重载。尽管限制多多,extern "C"函数仍是很是有用,由于它们能够象C函数同样被dlopen动态加载。冠以extern "C"限定符后,并不意味着函数中没法使用C++代码了,相反,它仍然是一个彻底的C++函数,可使用任何C++特性和各类类型的参数。

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

代码:app

//----------
//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';
}
ide

  在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的变量寻幽访胜以前,请细读“其余”一节中罗列的文档。

  加载类
  加载类有点困难,由于咱们须要类的一个实例,而不只仅是一个函数指针。咱们没法经过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);
}


//----------
//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;
}

  加载类时有一些值得注意的地方:
  ◆ 你必须(译者注:在模块或者说共享库中)同时提供一个创造函数和一个销毁函数,且不能在执行文件内部使用delete来销毁实例,只能把实例指针传递给模块的销毁函数处理。这是由于C++里头,new操做符能够被重载;这容易致使new-delete的不匹配调用,形成莫名其妙的内存泄漏和段错误。这在用不一样的标准库连接模块和可执行文件时也同样。
  ◆ 接口类的析构函数在任何状况下都必须是虚函数(virtual)。由于即便出错的可能极小,近乎杞人忧天了,但仍旧不值得去冒险,反正额外的开销微不足道。若是基类不须要析构函数,定义一个空的(但必须虚的)析构函数吧,不然你早晚要遇到问题,我向您保证。你能够在comp.lang.c++ FAQ( http://www.parashift.com/c++-faq-lite/ )的第20节了解到更多关于该问题的信息。

源代码
  你能够下载全部包含在本文档中的代码包: http://www.isotton.com/howtos/C++-dl...xamples.tar.gz

FAQ
(译者注:下文翻译暂时省略)
1.I'm using Windows and I can't find the dlfcn.h header file! What's the problem?

The problem is that Windows doesn't have the dlopen API, and thus there is no dlfcn.h header. There is a similar API around the LoadLibrary function, and most of what is written here applies to it, too. Please refer to the Microsoft Developer Network Website for more information.

2.Is there some kind of dlopen-compatible wrapper for the Windows LoadLibrary API?

I don't know of any, and I don't think there'll ever be one supporting all of dlopen's options.

There are alternatives though: libtltdl (a part of libtool), which wraps a variety of different dynamic loading APIs, among others dlopen and LoadLibrary. Another one is the Dynamic Module Loading functionality of GLib. You can use one of these to ensure better possible cross-platform compatibility. I've never used any of them, so I can't tell you how stable they are and whether they really work.

You should also read section 4, “Dynamically Loaded (DL) Libraries”, of the Program Library HOWTO for more techniques to load libraries and create classes independently of your platform.


其余* The dlopen(3) man page. It explains the purpose and the use of the dlopen API.* The article Dynamic Class Loading for C++ on Linux by James Norton published on the Linux Journal.* Your favorite C++ reference about extern "C", inheritance, virtual functions, new and delete. I recommend [STR2000].* [ISO14882]* The Program Library HOWTO, which tells you most things you'll ever need about static, shared and dynamically loaded libraries and how to create them. Highly recommended.* The Linux GCC HOWTO to learn more about how to create libraries with GCC.

相关文章
相关标签/搜索