可使用gcc或者g++编译器生成动态库文件(此处以g++编译器为例)
g++ -shared -fPIC -c XXX.cpp
g++ -shared -fPIC -o XXX.so XXX.o
动态库的调用关系可以在需要调用动态库的程序编译时,通过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之后,析构函数会被调用。
3、 普通函数的调用
此处以源码实例说明。各源码文件关系如下:
test_so1.h和test_so1.cpp生成test_so1.so动态库。
test_so2.h和test_so2.cpp生成test_so2.so动态库。
test_dl.cpp生成test_dl可执行程序,test_dl通过dlopen系列等API函数,并使用函数指针以到达动态调用不同so库中test函数的目的。
linux下的.so文件为共享库,相当于windows下的dll文件,使用方法如下:在你的工程源代码里包含.h头文件,然后可以调用动态库里的函数,在链接的时候加上如下编译器参数:
-l xx.so
如果你的so文件是以lib开头的,还可以直接这样使用:
-lxx
xx是你的.so文件名
其实使用方法和你使用数学库函数是一样的,源代码中添加
#include <math.h>,编译的时候,加上-lm参数。
直接上代码[User:root Time:16:46:48 Path:/home/liangdong/cpp]$ ll
total 12
-rw-r--r--. 1 root root 120 May 4 16:46 libtest.cpp
-rw-r--r--. 1 root root85 May 4 16:45 libtest.h
-rw-r--r--. 1 liangdong daemon 141 May 4 16:42 main.cpp
[User:root Time:16:46:48 Path:/home/liangdong/cpp]$ cat libtest.cpp libtest.h main.cpp
#include "libtest.h"
#include <iostream>
using namespace std
void test::function() {
cout<<"test::function"<<endl
}
#ifndef _H_TEST
#define _H_TEST
class test {
public:
void function()
}
#endif
#include <iostream>
#include "libtest.h"
using namespace std
int main(int argc, char* const argv[]) {
test t
t.function()
return 0
}
[User:root Time:16:46:55 Path:/home/liangdong/cpp]$ g++ -o libtest.so -fPIC -shared libtest.cpp -I.
[User:root Time:16:47:29 Path:/home/liangdong/cpp]$ g++ -o main main.cpp -L. -ltest
[User:root Time:16:47:48 Path:/home/liangdong/cpp]$ ll
total 28
-rw-r--r--. 1 root root120 May 4 16:46 libtest.cpp
-rw-r--r--. 1 root root 85 May 4 16:45 libtest.h
-rwxr-xr-x. 1 root root 5773 May 4 16:47 libtest.so
-rwxr-xr-x. 1 root root 5876 May 4 16:47 main
-rw-r--r--. 1 liangdong daemon 141 May 4 16:42 main.cpp
[User:root Time:16:47:50 Path:/home/liangdong/cpp]$ ./main
./main: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
[User:root Time:16:47:51 Path:/home/liangdong/cpp]$ export LD_LIBRARY_PATH=.
[User:root Time:16:48:01 Path:/home/liangdong/cpp]$ ./main
test::function
[User:root Time:16:48:04 Path:/home/liangdong/cpp]$
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)