linux下c++动态库的生成及使用
文章来源于:http://hi.baidu.com/ablenavy/item/b498901c6826bbf587ad4e33
我的程序是一个类,在网上找了半天,都是c的例子,c++的类封装成静态库倒容易,可是如何封装成动态库,在其它程序中调用呢?
Linux下的动态链接库叫so,即Shared Object,共享对象。一些函数就不说了,网上多的是。把我遇到的问题写下来吧
提示错误 undefined reference to `dlopen'
编译时增加“-ldl”选项即可解决。
提示错误 cannot open shared object file: No such file or directory
将当前目录的绝对路径添加到LD_LIBRARY_PATH即可
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/zhj/test_so/
提示错误 undefined reference to
检查了一下,原来在makefile里少包含了一个文件。
这一类错误一般都是缺少相应的类文件所致。
例子来源:
http://www.linuxsir.org/bbs/printthread.php?t=266890
这个扩展名为hpp的文件,我分解了一下,下面就上代码吧
polygon.h
//----------
//polygon.h:
//----------
#ifndef POLYGON_H
#define POLYGON_H
#include <stdio.h>
class polygon {
protected:
double side_length_;
public:
polygon();
virtual ~polygon();
void set_side_length(double side_length);
virtual double area() const;
};
// the types of the class factories
typedef polygon* create_t();
typedef void destroy_t(polygon*);
#endif
polygon.cpp
//----------
//polygon.cpp:
//----------
#include "polygon.h"
polygon::polygon()
{
//set_side_length(0);
}
polygon::~polygon()
{
}
void polygon::set_side_length(double side_length)
{
printf("polygon side_length: %f\n\n",side_length);
side_length_ = side_length;
printf("polygon side_length_: %f\n\n",side_length_);
}
double polygon::area() const
{
return 0;
}
triangle.cpp
//----------
//triangle.cpp:
//----------
#include "polygon.h"
#include <cmath>
class triangle : public polygon {
public:
virtual double area() const {
printf("triangle side_length_: %f\n\n",side_length_);
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;
}
main.cpp
//----------
//main.cpp:
//----------
#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;
}
// 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);
}
makefile
objects = main.o polygon.o
main: $(objects)
g++ -g -rdynamic -o $@ $(objects) -ldl
main.o: main.cpp
g++ -g -c main.cpp
polygon.o: polygon.cpp polygon.h
g++ -g -c polygon.cpp
.PHONY : clean
clean :
-rm main $(objects)
用下面的命令编译,生成libtriangle.so
g++ -g -fpic -shared -o libtriangle.so triangle.cpp polygon.cpp
然后make一下,运行main试试吧!
linux下c++动态库的生成及使用的更多相关文章
- linux下so动态库一些不为人知的秘密(转)
		
linux 下有动态库和静态库,动态库以.so为扩展名,静态库以.a为扩展名.二者都使用广泛.本文主要讲动态库方面知识.基本上每一个linux 程序都至少会有一个动态库,查看某个程序使用了那些动态库, ...
 - linux下so动态库一些不为人知的秘密
		
linux 下有动态库和静态库,动态库以.so为扩展名,静态库以.a为扩展名.二者都使用广泛.本文主要讲动态库方面知识. 基本上每一个linux 程序都至少会有一个动态库,查看某个程序使用了那些 ...
 - linux下so动态库一些不为人知的秘密(中二)
		
继续上一篇< linux下so动态库一些不为人知的秘密(中) >介绍so搜索路径,还有一个类似于-path,叫LD_RUN_PATH环境变量, 它也是把路径编译进可执行文件内,不同的是它只 ...
 - linux下so动态库一些不为人知的秘密(中)
		
上一篇(linux下so动态库一些不为人知的秘密(上))介绍了linux下so一些依赖问题,本篇将介绍linux的so路径搜索问题. 我们知道linux链接so有两种途径:显示和隐式.所谓显示就是程序 ...
 - linux下so动态库一些不为人知的秘密(上)
		
linux 下有动态库和静态库,动态库以.so为扩展名,静态库以.a为扩展名.二者都使用广泛.本文主要讲动态库方面知识. 基本上每一个linux 程序都至少会有一个动态库,查看某个程序使 ...
 - Linux下创建动态库与使用
		
参考文章:dll和so文件区别与构成:http://www.cnblogs.com/likwo/archive/2012/05/09/2492225.html 动态库路径配置- /etc/ld.so. ...
 - Linux下设置动态库的方法
		
库文件在连接(静态库和共享库)和运行(仅限于使用共享库的程序)时被使用,其搜索路径是在系统中进行设置的. 一般 Linux 系统把 /lib 和 /usr/lib 两个目录作为默认的库搜索路径,所以使 ...
 - linux下so动态库一些不为人知的秘密 系列
		
http://blog.chinaunix.net/uid-27105712-id-3313293.html http://www.cnblogs.com/gulvzhe/archive/2012/0 ...
 - linux下的静态库和动态库
		
一.linux下的静态库 静态库中的被调用的函数的代码会在编译时一起被复制到可执行文件中去的!!可执行文件在运行不需要静态库的存在! 二.linux下动态库的构建和使用 1.动态库的构建 ...
 
随机推荐
- MySQL性能分析及explain的使用说明
			
1.使用explain语句去查看分析结果 如explain select * from test1 where id=1;会出现:id selecttype table type possible_k ...
 - Object类可以接受引用类型
			
Object类是一切类的父类,所以Object类可以接受一切引用类型.连数组和接口对象也都可以接受. 1.接受数组 public class ObjectTest{ public static voi ...
 - Python collections.defaultdict() 与 dict的使用和区别
			
看样子这个文档是难以看懂了.直接看示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import collections s = [('yellow', ...
 - phpmyadmin修改mysql数据库密码
			
甩上链接:http://jingyan.baidu.com/article/e4511cf332b9832b845eaf27.html
 - Spring Boot Web Executable Demo
			
Spring Boot Web Executable Demo */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.sr ...
 - fastreport.net  cdoe  自己的代码
			
//初始 Report report1 = new Report(); report1.Clear(); string Re ...
 - iOS 错误之 NSObject 、CGFloat
			
需要添加 #import <Foundation/Foundation.h> #import <UIKit/UIKit.h>
 - UVa 129 困难的串
			
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
 - 如何在Eclipse中配置python开发环境
			
考虑到网上关于Eclipse中配置Python开发环境的文章千篇一律,故写此文以总结. 本文主要内容是:三种Pydev配置方法和一种PyDev卸载方法. 本文的前提是你已经安装了Eclipse和pyt ...
 - jq hide show
			
var $ = function (id) { return document.getElementById(id); } //返回dom元素的当前某css值 var getCss = functio ...