C++ dlopen使用

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值