本文主要是介绍linux动态库类的调用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://blog.csdn.net/zzd121333/article/details/51684262
关于动态调用动态库方法说明
一、 动态库概述
1、 动态库的概念
日常编程中,常有一些函数不需要进行编译或者可以在多个文件中使用(如数据库输入/输出操作或屏幕控制等标准任务函数)。可以事先对这些函数进行编译,然后将它们放置在一些特殊的目标代码文件中,这些目标代码文件就称为库。库文件中的函数可以通过连接程序与应用程序进行链接,这样就不必在每次开发程序时都对这些通用的函数进行编译了。
动态库是一种在已经编译完毕的程序开始启动运行时,才被加载来调用其中函数的库。其加载方式与静态库截然不同。
2、 动态库的命名
Linux下,动态库通常以.so(share object)结尾。(通常/lib和/usr/lib等目录下存在大量系统提供的以.so结尾的动态库文件)
Windows下,动态库常以.dll结尾。(通常C:\windows\System32等目录下存在大量系统提供的以.dll结尾的动态库文件)
3、 动态库与静态库之间的区别
静态库是指编译连接时,把库文件的代码全部加入到可执行文件中,所以生成的文件较大,但运行时,就不再需要库文件了。即,程序与静态库编译链接后,即使删除静态库文件,程序也可正常执行。
动态库正好相反,在编译链接时,没有把库文件的代码加入到可执行文件中,所以生成的文件较小,但运行时,仍需要加载库文件。即,程序只在执行启动时才加载动态库,如果删除动态库文件,程序将会因为无法读取动态库而产生异常。
二、 Linux下动态调用动态库
备注:以下linux实例说明都是在RedHat 5.1系统+ gcc 版本 4.1.2 20080704 (Red Hat 4.1.2-46)上实现。
1、 .so动态库的生成
可使用gcc或者g++编译器生成动态库文件(此处以g++编译器为例)
g++ -shared -fPIC -c XXX.cpp
g++ -shared -fPIC -o XXX.so XXX.o
2、 .so动态库的动态调用接口函数说明
动态库的调用关系可以在需要调用动态库的程序编译时,通过g++的-L和-l命令来指定。例如:程序test启动时需要加载目录/root/src/lib中的libtest_so1.so动态库,编译命令可照如下编写执行:
g++ -g -o test test.cpp –L/root/src/lib –ltest_so1
(此处,我们重点讲解动态库的动态调用的方法,关于静态的通过g++编译命令调用的方式不作详细讲解,具体相关内容可上网查询)
Linux下,提供专门的一组API用于完成打开动态库,查找符号,处理出错,关闭动态库等功能。
下面对这些接口函数逐一介绍(调用这些接口时,需引用头文件#include <dlfcn.h>):
1) dlopen
函数原型:void *dlopen(const char *libname,int flag);
功能描述:dlopen必须在dlerror,dlsym和dlclose之前调用,表示要将库装载到内存,准备使用。如果要装载的库依赖于其它库,必须首先装载依赖库。如果dlopen操作失败,返回NULL值;如果库已经被装载过,则dlopen会返回同样的句柄。
参数中的libname一般是库的全路径,这样dlopen会直接装载该文件;如果只是指定了库名称,在dlopen会按照下面的机制去搜寻:
a.根据环境变量LD_LIBRARY_PATH查找
b.根据/etc/ld.so.cache查找
c.查找依次在/lib和/usr/lib目录查找。
flag参数表示处理未定义函数的方式,可以使用RTLD_LAZY或RTLD_NOW。RTLD_LAZY表示暂时不去处理未定义函数,先把库装载到内存,等用到没定义的函数再说;RTLD_NOW表示马上检查是否存在未定义的函数,若存在,则dlopen以失败告终。
2) dlerror
函数原型:char *dlerror(void);
功能描述:dlerror可以获得最近一次dlopen,dlsym或dlclose操作的错误信息,返回NULL表示无错误。dlerror在返回错误信息的同时,也会清除错误信息。
3) dlsym
函数原型:void *dlsym(void *handle,const char *symbol);
功能描述:在dlopen之后,库被装载到内存。dlsym可以获得指定函数(symbol)在内存中的位置(指针)。如果找不到指定函数,则dlsym会返回NULL值。但判断函数是否存在最好的方法是使用dlerror函数,
4) dlclose
函数原型:int dlclose(void *);
功能描述:将已经装载的库句柄减一,如果句柄减至零,则该库会被卸载。如果存在析构函数,则在dlclose之后,析构函数会被调用。
二、动态加载类
加载类有点困难,因为我们需要类的一个实例,而不仅仅是一个函数指针。我们无法通过new来创建类的实例,因为类是在动态库中定义的而不是在可执行程序中定义的,况且有时候我们连动态库中具体的类的名字都不知道。解决方案是:利用多态性!我们在可执行文件中定义一个带虚成员函数的接口基类,而在模块中定义派生实现类。通常来说,接口类是抽象的(如果一个类含有虚函数,那它就是抽象的)。因为动态加载类往往用于实现插件,这意味着必须提供一个清晰定义的接口──我们将定义一个接口类和派生实现类。
接下来,在模块中,我们会定义两个附加的类工厂函数(class factory functions)(或称对象工厂函数)。其中一个函数创建一个类实例,并返回其指针;另一个函数则用以销毁该指针。这两个函数都以extern "C"来限定修饰。
实例如下:
testBase.h中定义一个含有纯虚函数virtual void display() const = 0的基类。
test1.cpp中定义继承类test1,并实现虚函数virtual void display() const的定义,并实现一个创建类函数和一个销毁类指针函数。
test2.cpp中定义继承类test2,并实现虚函数virtual void display() const的定义,并实现一个创建类函数和一个销毁类指针函数。
main.cpp中实现动态的调用不同库中的display()方法。
testBase.h
- #ifndef __TESTBASE_H__
- #define __TESTBASE_H__
- class testBase
- {
- public:
- testBase(){}
- virtual ~testBase(){}
- virtual void display() = 0;
- };
- typedef testBase* create_t();
- typedef void destroy_t(testBase *p);
- #endif
#ifndef __TESTBASE_H__
#define __TESTBASE_H__class testBase
{
public:testBase(){}virtual ~testBase(){}virtual void display() = 0;
};typedef testBase* create_t();
typedef void destroy_t(testBase *p);
#endif
test1.cpp
- </pre><pre name="code" class="cpp">#include<stdio.h>
- #include "testBase.h"
- class test1:public testBase
- {
- public:
- void display()
- {
- printf("display in test1\n");
- }
- };
- extern "C" testBase* create()
- {
- return new test1;
- }
- extern "C" void destroy(testBase *p)
- {
- delete p;
- }
</pre><pre name="code" class="cpp">#include<stdio.h>
#include "testBase.h"class test1:public testBase
{
public:void display(){printf("display in test1\n");}
};extern "C" testBase* create()
{return new test1;
} extern "C" void destroy(testBase *p)
{delete p;
}
test2.cpp
- #include<stdio.h>
- #include "testBase.h"
- class test2:public testBase
- {
- public:
- void display()
- {
- printf("display in test2\n");
- }
- };
- extern "C" testBase* create()
- {
- return new test2;
- }
- extern "C" void destroy(testBase *p)
- {
- delete p;
- }
#include<stdio.h>
#include "testBase.h"class test2:public testBase
{
public:void display(){printf("display in test2\n");}
};extern "C" testBase* create()
{return new test2;
} extern "C" void destroy(testBase *p)
{delete p;
}
main.cpp
- #include<stdio.h>
- #include"testBase.h"
- #include<dlfcn.h>
- int main()
- {
- void *handle;
- char *error;
- handle = dlopen("./libtest2.so", RTLD_LAZY);
- if (!handle)
- {
- printf("%s\n", dlerror());
- return -1;
- }
- create_t* create = (create_t *)dlsym(handle, "create");
- if ( (error = dlerror()) != NULL)
- {
- printf("%s\n", error);
- return -1;
- }
- destroy_t* destroy = (destroy_t *)dlsym(handle, "destroy");
- if ( (error = dlerror()) != NULL)
- {
- printf("%s\n", error);
- return -1;
- }
- testBase *test = create();
- test->display();
- destroy(test);
- dlclose(handle);
- return 0;
- }
#include<stdio.h>
#include"testBase.h"
#include<dlfcn.h>int main()
{void *handle;char *error;handle = dlopen("./libtest2.so", RTLD_LAZY);if (!handle){printf("%s\n", dlerror());return -1;}create_t* create = (create_t *)dlsym(handle, "create");if ( (error = dlerror()) != NULL){printf("%s\n", error);return -1;}destroy_t* destroy = (destroy_t *)dlsym(handle, "destroy");if ( (error = dlerror()) != NULL){printf("%s\n", error);return -1;}testBase *test = create();test->display();destroy(test);dlclose(handle);return 0;
}
编译:
g++ test1.cpp -fPIC -shared -olibtest1.so
g++ test2.cpp -fPIC -shared -olibtest2.so
g++ main.cpp -ldl -ltest1 -L./
三、总结:
1、必须使用 extern "C"声明的函数将使用函数名作符号名,就像C函数一样。因此,只有非成员函数才能被声明为extern "C",并且不能被重载。尽管限制多多,extern "C"函数还是非常有用,因为它们可以象C函数一样被dlopen动态加载。冠以extern "C"限定符后,并不意味着函数中无法使用C++代码了,相反,它仍然是一个完全的C++函数,可以使用任何C++特性和各种类型的参数。所以extern "C" 只是告诉编译器编和链接的时候都用c的方式的函数名字,函数里的内容可以为c的代码也可以为c++的。
2、链接时注意加动态连接参数-ldl
3、链接时要指定动态库路径(-L./)不然会会找不到动态库:cannot find -ltest1
4、运行时程序会提示如下错误
error while loading shared libraries: libtiger.so: cannot open shared object file: No such file or direct
这是因为程序运行时没有找到动态链接库造成的。程序编译时链接动态库和运行时使用动态链接库的概念是不同的,在运行时,程序链接的动态链接库需要在系统目录下才行。
使用以下方法可以解决此问题
a. 在linux下最方便的解决方案是拷贝libtest1.so到绝对目录 /lib 下(但是,要是超级用户才可以,因此要使用sudo哦,亲)。就可以生成可执行程序了
b.第二种方法是:将动态链接库的目录放到程序搜索路径中,可以将库的路径加到环境变量LD_LIBRARY_PATH中实现:
export LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH
这篇关于linux动态库类的调用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!