Linux shared library-- Loading class
2010-04-26 16:58
218 查看
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;
}
//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 function
- WebSphere Class Loaders and Shared Library, Part 3 (Class loader in WebSphre Portal + Shared Library )
- Linux shared library usage detail
- Linux系统共享库编程- linux shared library 编程入门
- Dynamic Class Loading for C++ on Linux
- Linux中error while loading shared libraries错误解决办法
- 解决linux运行某程序时提示error while loading shared libraries: abc.so cannot open shared object file
- Linux中error while loading shared libraries错误解决办法
- error while loading shared libraries: libstdc++.so.6: wrong ELF class: ELFCLASS32
- linux加载共享问题error while loading shared libraries: xxx.so.0:cannot open shared object file: No such fi
- WebSphere Class Loaders and Shared Library, Part 4 (Answer to case scenario + troubleshooting)
- 如何解决Linux:error while loading shared libraries: libmpc.so.3(不使用root权限)
- error while loading shared libraries: libcudart.so.3: wrong ELF class: ELFCLASS32 的解决
- 解决 Linux error while loading shared libraries: cannot open shared object file: No such file or directory
- error while loading shared libraries: liblzo2.so.2: wrong ELF class: ELFCLASS64
- Shared Library Search Paths On Linux
- Linux下redis编程:error while loading shared libraries: libhiredis.so.0.11
- linux执行程序:error while loading shared libraries
- Linux下安装oracle关于UnsatisfiedLinkError exception loading native library:njni10报错解决
- 实用方便解决error while loading shared libraries办法,及linux gcc 下使用总结