Linux shared library-- Loading class
来源:互联网 发布:网络基础知识培训 编辑:程序博客网 时间:2024/05/17 09:09
Linked from: http://www.faqs.org/docs/Linux-mini/C++-dlopen.html
//1.==== main.cpp =============================================================
// How to build?
// g++ main.cpp -ldl
//=============================================================================
#include "polygon.h"
#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;
}
// 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);
}
//2.=====polygon.h======================================
#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
//3.=====triangle.cpp=================================
// How to build?
// g++ -c -fPIC triangle.cpp
// g++ -shared -o triangle.so -fPIC triangle.o
//=================================================
#include "polygon.h"
#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;
}
- Linux shared library-- Loading class
- Linux shared library-- Loading function
- linux shared library
- linux so shared library
- FreeBSD/Linux shared library dependency
- Linux shared library usage detail
- Oracle db: error with loading shared library libodm9.so
- An Application Program Dynamically Loading and Linking the Shared Library
- WebSphere Class Loaders and Shared Library, Part 3 (Class loader in WebSphre Portal + Shared Library )
- WebSphere Class Loaders and Shared Library, Part 2 (Class Loaders)
- Shared Library Search Paths On Linux
- shared library search path set on linux
- WebSphere Class Loaders and Shared Library, Part 1 (Intention)
- 执行Linux程序时,报“error while loading shared libraries ×××”
- [Linux] rpm :error while loading shared librarieszlib.so.1
- linux执行命令之后显示error while loading shared libraries
- Linux error while loading shared libraries的解決步骤
- error while loading shared libraries: libopencv_highgui.so.2.4 linux
- IN-EXISTS
- Android自动化测试初探(一): 捕获Activity上的Element
- 生命之道
- 免費PDF批量转换工具
- 探查“ORA-01000:超出最多允许打开的游标数” - Oracle
- Linux shared library-- Loading class
- 文件下载
- State模式(状态模式)消除烦琐的if..else语句
- 多線程訪問界面(轉)
- C#反射和抽象工厂的结合
- 支持鼠标双击事件的DataGrid
- eclipse快捷键大全
- ogre中的鼠标拾取
- Messagebox的Owner,显示位置,自动关闭的研究。