中国网站建设总部在哪里,广州公关公司有哪些,海口 网站 制作,新乡优化2019独角兽企业重金招聘Python工程师标准 关于动态调用动态库方法说明 一、 动态库概述 1、 动态库的概念 日常编程中#xff0c;常有一些函数不需要进行编译或者可以在多个文件中使用#xff08;如数据库输入/输 出操作或屏幕控制等标准任务函数#… 2019独角兽企业重金招聘Python工程师标准 关于动态调用动态库方法说明 一、 动态库概述 1、 动态库的概念 日常编程中常有一些函数不需要进行编译或者可以在多个文件中使用如数据库输入/输 出操作或屏幕控制等标准任务函数。可以事先对这些函数进行编译然后将它们放置在一些特殊的目标代码文件中这些目标代码文件就称为库。库文件中的函数 可以通过连接程序与应用程序进行链接这样就不必在每次开发程序时都对这些通用的函数进行编译了。 动态库是一种在已经编译完毕的程序开始启动运行时才被加载来调用其中函数的库。其加载方式与静态库截然不同。 2、 动态库的命名 Linux下动态库通常以.so(shareobject)结尾。(通常/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用于完成打开动态库查找符号处理出错关闭动态库等功能。 下面对这些接口函数逐一介绍调用这些接口时需引用头文件#includedlfcn.h) 1) dlopen 函数原型void *dlopen(const char *libname,int flag); 功能描述dlopen必须在dlerrordlsym和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函数的目的。 test_so1.h// #include stdio.h #include stdlib.h extern C { int test(void); } ttest_so1.cpp// #include test_so1.h int test(void) { printf(USING TEST_SO1.SO NOW!\n);//注意此处与test_so2.cpp中的 //test函数的不同 return 1; } test_so2.h// #include stdio.h #include stdlib.h extern C { int test(void); } ttest_so2.cpp// #include test_so2.h int test(void) { printf(USING TEST_SO2.SO NOW!\n);//注意此处与test_so1.cpp中的 //test函数的不同 return 1; } test_dl.cpp// #include stdio.h #include stdlib.h #include dlfcn.h int main(int argc, char **argv) { if(argc!2) { printf(Argument Error! You must enter like this:\n); printf(./test_dl test_so1.so\n); exit(1); } void *handle; char *error; typedef void (*pf_t)(); //声明函数指针类型 handle dlopen (argv[1],RTLD_NOW); //打开argv[1]指定的动态库 if (!handle) { fprintf (stderr, %s\n, dlerror()); exit(1); } dlerror(); pf_tpf(pf_t)dlsym(handle,test); //指针pf指向test在当前内存中的地址 if ((error dlerror()) ! NULL) { fprintf (stderr, %s\n, error); exit(1); } pf(); //通过指针pf的调用来调用动态库中的test函数 dlclose(handle); //关闭调用动态库句柄 return 0; } makefile// .SUFFIXES: .c .cpp .o CCg -shared -fPIC GCCg all:test_so1.so test_so2.so test_dl clean OBJ1test_so1.o OBJ2test_so2.o OBJ3test_dl.o test_so1.so:$(OBJ1) $(CC) -o $ $? cp $ /usr/lib test_so2.so:$(OBJ2) $(CC) -o $ $? cp $ /usr/lib test_dl:$(OBJ3) $(GCC) -o $ $? -ldl .cpp.o: $(CC) -c $*.cpp .c.o: $(CC)-c $*.c clean: rm -f *.o 上述源程序中需重点注意两个问题 1、test_dl.cpp中对于动态库中的test函数调用是通过函数指针来完成的。 2、test_so1.h和test_so2.h中都使用了extern C。 在每个C程序或库、目标文件中所有非静态non-static函数在二进制文件中都是以“符号symbol”形式出现的。这些符号都是唯一的字符串从而把各个函数在程序、库、目标文件中区分开来。 在C中符号名正是函数名strcpy函数的符号名就是“strcpy”。这可能是因为两个非静态函数的名字一定各不相同的缘故。 而C允许重载不同的函数有相同的名字但不同的参数并且有很多C所没有的特性 ──比如类、成员函数、异常说明──几乎不可能直接用函数名作符号名。为了解决这个问题C采用了所谓的namemangling。它把函数名和一些 信息如参数数量和大小杂糅在一起改造成奇形怪状只有编译器才懂的符号名。例如被mangle后的foo可能看起来像foo4%6^或者符 号名里头甚至不包括“foo”。 其中一个问题是C标准目前是[ISO14882]并没有定义名字必须如何被 mangle所以每个编译器都按自己的方式来进行namemangling。有些编译器甚至在不同版本间更换mangling算法尤其是g2.x 和3.x。即使您搞清楚了您的编译器到底怎么进行mangling的从而可以用dlsym调用函数了但可能仅仅限于您手头的这个编译器而已而无法 在下一版编译器下工作。 用 extern C声明的函数将使用函数名作符号名就像C函数一样。因此只有非成员函数才能被声明为externC并且不能被重载。尽管限制多 多externC函数还是非常有用因为它们可以象C函数一样被dlopen动态加载。冠以externC限定符后并不意味着函数中无法使用 C代码了相反它仍然是一个完全的C函数可以使用任何C特性和各种类型的参数。 执行makefile正常编译后可生成test_so1.so、test_so2.so动态库以及test_dl执行程序。可执行test_dl显示结果如下 [rootlocalhost so_src]# ./test_dl test_so1.so USING TEST_SO1.SO NOW! [rootlocalhost so_src]# ./test_dl test_so2.so USING TEST_SO2.SO NOW! [rootlocalhost so_src]# ./test_dl Argument Error! You must enter like this: ./test_dl test_so1.so 备注如果我们去掉test_so1.h和test_so2.h中的externC重新编译执行后将可能会出现什么情况有兴趣的朋友可以试下 [rootlocalhost so_src]# ./test_dl test_so1.so /usr/lib/test_so1.so: undefined symbol: test [rootlocalhost so_src]# ./test_dl test_so2.so /usr/lib/test_so2.so: undefined symbol: test 4、 类的调用 加载类有点困难因为我们需要类的一个实例而不仅仅是一个函数指针。我们无法通过new来创建类的实例因为类是在动态库中定义的而不是在可执行程序中定义的况且有时候我们连动态库中具体的类的名字都不知道。 解决方案是利用多态性我们在可执行文件中定义一个带虚成员函数的接口基类而在模 块中定义派生实现类。通常来说接口类是抽象的如果一个类含有虚函数那它就是抽象的。因为动态加载类往往用于实现插件这意味着必须提供一个清晰定 义的接口──我们将定义一个接口类和派生实现类。 接下来在模块中我们会定义两个附加的类工厂函数class factoryfunctions或称对象工厂函数。其中一个函数创建一个类实例并返回其指针另一个函数则用以销毁该指针。这两个函数都以externC来限定修饰。 实例如下 test_base.hpp中定义一个含有纯虚函数virtual void display() const 0的基类。 test_1.cpp中定义继承类test1并实现虚函数virtual void display()const的定义并实现一个创建类函数和一个销毁类指针函数。 test_2.cpp中定义继承类test2并实现虚函数virtual void display()const的定义并实现一个创建类函数和一个销毁类指针函数。 main.cpp中实现动态的调用不同库中的display()方法。 test_base.hpp// #ifndef TEST_BASE_HPP #define TEST_BASE_HPP #include iostream using namespace std; class test_base { public: test_base(){} virtual~test_base() {} voidcall_base() { cout call base endl; } virtualvoid display() const 0 ; }; // the types of the class factories typedef test_base* create_t(); typedef void destroy_t(test_base*); #endif test1.cpp// #include test_base.hpp class test1 : public test_base { public: virtualvoid display() const { cout Running in test1.so Now endl; } }; // the class factories extern C test_base* create() { returnnew test1; } extern C void destroy(test_base* p) { deletep; } test1.cpp// #include test_base.hpp class test2 : public test_base { public: virtualvoid display() const { cout Running in test2.so Now endl; } }; // the class factories extern C test_base* create() { returnnew test2; } extern C void destroy(test_base* p) { deletep; } main.cpp// #include test_base.hpp #include iostream #include dlfcn.h int main(int argc , char** argv) { // loadthe test library if(argc!2) { cout Argument Error! You mustenter like this: \n; cout ./a.out test_1.so \n; return 1; } void*test_index dlopen(argv[1], RTLD_NOW); if(!test_index) { cerr Cannot load library: dlerror() \n; return 1; } // reseterrors dlerror(); // loadthe symbols create_t*create_test (create_t*) dlsym(test_index, create); constchar* dlsym_error dlerror(); if(dlsym_error) { cerr Cannot load symbol create: dlsym_error \n; return 1; } destroy_t* destroy_test (destroy_t*) dlsym(test_index,destroy); dlsym_error dlerror(); if(dlsym_error) { cerr Cannot load symbol destroy: dlsym_error \n; return 1; } // createan instance of the class test_base* c_test create_test(); // usethe class c_test-display(); destroy_test(c_test); // unloadthe test library dlclose(test_index); } makefile// .SUFFIXES: .c .cpp .o CCg -g -shared -fPIC GCCg -g all:clear test_1.so a.out test_2.so clean OBJ1test_1.o OBJ2main.o OBJ3test_2.o clear: rm -rf *.so a.out b.out test_1.so:$(OBJ1) $(CC) -o $ $? cp $ /usr/lib a.out:$(OBJ2) $(GCC) -o $ $? -ldl test_2.so:$(OBJ3) $(CC) -o $ $? cp $ /usr/lib .cpp.o: $(CC) -c $*.cpp .c.o: $(CC) -c $*.c clean: rm -f *.o 执行makefile正常编译后可生成test_1.so、test_2.so动态库以及a.out执行程序。可执行a.out显示结果如下 [rootlocalhost c_so_src]# ./a.out test_1.so Running in test1.so Now [rootlocalhost c_so_src]# ./a.out test_2.so Running in test2.so Now [rootlocalhost c_so_src]# ./a.out Argument Error! You must enter like this: ./a.out test_1.so 三、 Windows下动态调用动态库 备注以下windows实例说明都是在Win7系统visual studio2005上实现。 1、 .dll动态库的生成 使用visual studio2005工具创建一个新项目选择Win32——Win32控制台应用程序(此处需选择名称及位置)——应用程序类型DLL附加选项空项目完成以上步骤即可创建一个dll项目。 在项目中的头文件和源文件、资源文件中新增相应代码后通过工具栏中Build(生成)即可生成相应dll文件。dll文件生成的位置通常在该项目位置中的debug目录下。 2、 .dll动态库的动态调用接口函数说明 1) LoadLibrary 函数原型HMODUBLE WINAPI LoadLibrary(LPCTSTR lpFileName); 其中HMODUBLE通常是被载入模块的线性地址类型LPCTSTR const tchar *。 功能描述表示要将库装载到内存准备使用。如果要装载的库依赖于其它库必须首先装载依赖库。如果LoadLibrary操作失败返回NULL值如果库已经被装载过则LoadLibrary会返回同样的句柄。 参数中的lpFileName一般是库的全路径这样LoadLibrary会直接装载该文件如果只是指定了库名称在LoadLibrary会在当前目录下查找。 2) GetProcAddress 函数原型FARPROC WINAPI GetProcAddress (HMODUBLEhModule,LPCTSTR lpProcName); 其中FARPROC 通常代表函数指针 功能描述表示已获取指向应用程序要调用的每个导出函数的函数指针。由于应用程序是通过指针调用 DLL的函数编译器不生成外部引用故无需与导入库链接。 参数中的hModule是由LoadLibrary加载库后返回的模块线性地址句柄lpProcName是要调用的库函数名称。 3) GetProcAddress 函数原型 BOOL WINAPI FreeLibrary(HMODUBLE hModule) 功能描述使用完 DLL 后调用FreeLibrary卸载动态库。卸载成功返回true否则返回false。 3、 普通函数的调用 使用visual studio2005工具创建一个新项目选择Win32——Win32控制台应用程序(此处需选择名称及位置假设该处名称为dll_load)—— 应用程序类型控制台应用程序附加选项预编译头完成以上步骤即可创建一个dll_load项目。 创建dll_load项目完毕后修改项目的字符集属性步骤如下 项目——dll_load属性(最后一行就是)——配置属性——常规——字符集设置 为“未设置”。项目默认创建的字符集为“使用UNICODE字符集”。如果字符集设置为UNICODE字符集的话调试程序时无法自动实现“char *”转换为“LPCWSTR”需使用_T()或其它方法解决 然后在该dll_load项目中继续添加dll1和dll2项目添加步骤如下 文件——添加——新建项目——Win32——Win32控制台应用程序(此处填写名称dll1位置默认)——应用程序类型DLL附加选项空项目。 完成以上步骤即可在当前dll_deal项目中增加dll1项目。dll2项目也可参照dll1项目的添加即可。 在dll_load、dll1和dll2项目中增加下图.h和.cpp源程序文件(其中dll_deal中的stdafx.h和stdafx.cpp为项目创建时默认生成无需增加)。 各源程序文件代码如下 dll1.h/dll1.cpp声明定义int test()方法并生成dll1.dll动态库。 dll2.h/dll2.cpp声明定义int test()方法并生成dll2.dll动态库。 dll_load.cpp中实现调用不同动态库的test()方法。 dll_deal.cpp// // dll_load.cpp : 定义控制台应用程序的入口点。 // #include stdafx.h #includestdio.h #includewindows.h #includewinuser.h #includetchar.h #includestdlib.h typedef int(*lpFun)(); //定义函数指针类型 int main() { HINSTANCE hDll; //DLL句柄 lpFun testFun; //函数指针 char *dll_name(char *)malloc(1024); printf(Please choose the dll_name(dll1.dll or dll2.dll):\n); scanf(%s,dll_name); printf(\n); hDll LoadLibrary(dll_name);//加载DLL,需要将DLL放到工程目录下. free(dll_name); if (hDll ! NULL) { printf(LOAD DLL success\n); testFun (lpFun)GetProcAddress(hDll, test); if (testFun ! NULL) { testFun(); } else { printf(the calling is error\n); } FreeLibrary(hDll); } else { printf(Load DLL Error or DLL not exist!\n); } return 0; } dll1.h// #ifdef DLL1_API #else #define DLL1_API extern C_declspec(dllimport) //同.cpp文件中同步 #endif DLL1_API int test(); //表明函数是从DLL导入给客户端使用 dll1.cpp// #include dll1.h #includestdlib.h #includestdio.h int test() { printf(RUNNING in dll1.dll NOW\n); return 0; } dll2.h// #ifdef DLL2_API #else #define DLL2_API extern C_declspec(dllimport) //同.cpp文件中同步 #endif DLL2_API int test(); //表明函数是从DLL导入给客户端使用 dll2.cpp// #include dll2.h #includestdlib.h #includestdio.h int test() { printf(RUNNING in dll2.dll NOW\n); return 0; } 各源程序中代码填充完成之后在dll1项目中完成dll1.dll的生成在dll2项目中完成dll2.dll的生成在dll_load项目中进行Debug结果如下 输入dll1.dll或者dll2.dll后结果如下 输入其它无效dll后结果如下 4、 类的调用 使用visual studio2005工具创建一个新项目选择Win32——Win32控制台应用程序(此处需选择名称及位置假设该处名称为dll_deal)—— 应用程序类型控制台应用程序附加选项预编译头完成以上步骤即可创建一个dll_deal项目。 创建dll_deal项目完毕后修改项目的字符集属性步骤如下 项目——dll_deal属性(最后一行就是)——配置属性——常规——字符集设置 为“未设置”。项目默认创建的字符集为“使用UNICODE字符集”。如果字符集设置为UNICODE字符集的话调试程序时无法自动实现“char *”转换为“LPCWSTR”需使用_T()或其它方法解决 然后在该dll_deal项目中继续添加dll1和dll2项目添加步骤如下 文件——添加——新建项目——Win32——Win32控制台应用程序(此处填写名称dll1位置默认)——应用程序类型DLL附加选项空项目。 完成以上步骤即可在当前dll_deal项目中增加dll1项目。dll2项目也可参照dll1项目的添加即可。 在dll_deal、dll1和dll2项目中增加下图.h和.cpp源程序文件(其中dll_deal中的stdafx.h和stdafx.cpp为项目创建时默认生成无需增加)。 各源程序文件代码如下 dll_deal.h/dll1.h/dll2.h中定义相同的含有纯虚函数virtual void display() const 0的基类。 dll1.cpp中定义继承类test1并实现虚函数virtual void display()const的定义并实现一个创建类函数和一个销毁类指针函数。 dll2.cpp中定义继承类test2并实现虚函数virtual void display()const的定义并实现一个创建类函数和一个销毁类指针函数。 dll_deal.cpp中实现调用不同动态库的display()方法。 dll_deal.h// #ifndef DLL_DEAL_H #define DLL_DEAL_H #includeiostream using namespace std; class test_base { public: test_base(){} virtual~test_base() {} voidcall_base() { cout call base endl; } virtual voiddisplay() const 0 ; }; // the types of the class factories typedef test_base* create_t(); typedef void destroy_t(test_base*); #endif dll_deal.cpp// // dll_deal.cpp : 定义控制台应用程序的入口点。 // #include stdafx.h #include string #includeiostream #includewindows.h #includewinuser.h #include dll_deal.h int main() { HINSTANCE hDll; //DLL句柄 string dll_name; cout Please choose thedll_name(dll1.dll or dll2.dll): endl; cin dll_name; cout endl; hDll LoadLibrary(dll_name.c_str());//加载DLL,需要将DLL放到工程目录下. if (hDll ! NULL) { cout LOAD DLL success! endl; // load the symbols create_t* create_test (create_t*)GetProcAddress(hDll,create); if (create_test NULL) { cout Cannot load symbol create: endl; return 1; } destroy_t* destroy_test (destroy_t*)GetProcAddress(hDll,destroy); if (destroy_test NULL) { cout Cannot load symbol destroy: endl; return 1; } // create an instance of the class test_base* c_test create_test(); // use the class c_test-display(); // destroy the class destroy_test(c_test); // unload the library FreeLibrary(hDll); } else { cout Load DLL Error or DLL notexist! endl; } return 0; } dll1.h// #ifndef DLL1_H #define DLL1_H #includeiostream using namespace std; class test_base { public: test_base(){} virtual~test_base() {} voidcall_base() { cout call base endl; } virtual voiddisplay() const 0 ; }; // the types of the class factories typedef test_base* create_t(); typedef void destroy_t(test_base*); #endif dll1.cpp// #include dll1.h #include cmath class test1 : public test_base { public: virtual voiddisplay() const { cout Running in test1.so Now endl; } }; // the class factories extern C __declspec(dllexport) test_base* create() { return newtest1; } extern C __declspec(dllexport) void destroy(test_base* p) { deletep; } dll2.h// #ifndef DLL2_H #define DLL2_H #includeiostream using namespace std; class test_base { public: test_base(){} virtual~test_base() {} voidcall_base() { cout call base endl; } virtual voiddisplay() const 0 ; }; // the types of the class factories typedef test_base* create_t(); typedef void destroy_t(test_base*); #endif dll2.cpp// #include dll2.h #include cmath class test2 : public test_base { public: virtual voiddisplay() const { cout Running in test2.so Now endl; } }; // the class factories extern C __declspec(dllexport) test_base* create() { return newtest2; } extern C __declspec(dllexport) void destroy(test_base* p) { deletep; } 各源程序中代码填充完成之后在dll1项目中完成dll1.dll的生成在dll2项目中完成dll2.dll的生成在dll_deal项目中进行Debug结果如下 输入dll1.dll或者dll2.dll后结果如下 输入其它无效dll后结果如下 转载于:https://my.oschina.net/u/994235/blog/342602