Linux操作系统下动态库的编写与调用.doc_第1页
Linux操作系统下动态库的编写与调用.doc_第2页
Linux操作系统下动态库的编写与调用.doc_第3页
Linux操作系统下动态库的编写与调用.doc_第4页
Linux操作系统下动态库的编写与调用.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1.用c语言写动态库:/* libsthc.h* Declarations for function add*/#include stdio.h#include stdlib.h#include stdarg.h#ifdef _cplusplusextern C#endifint add(int x, int y);#ifdef _cplusplus#endif/* libsthc.c* Implementation of function add declared in libsthc.h* in c language*/#include libsthc.hint add(int x, int y)return x + y;#makefilelibsthc.so:libsthc.ogcc -shared libsthc.o -lc -o libsthc.solibsthc.o:libsthc.c libsthc.hgcc -fPIC -c libsthc.c -o libsthc.oall:libsthc.soclean:rm -f *.o *.somake完成后,会生成一个动态库,即libsthc.so。为了使其他程序也可以使用该动态库,需要将库文件libsthc.so拷贝到/usr/lib目录下(由于权限的问题,一般要以root的身分进行拷贝),为了使其他程序也可以使用该动态库,需要将头文件libsthc.h拷贝到/usr/include目录下(由于权限的问题,一般要以root的身分进行拷贝)。1.1用c语言静态方式调用动态库libsthc.so:/* ctest.c* Testing program for libsthc.so library* in c languange* by玄机逸士*/#include libsthc.hint main(void)printf(%dn, add(1, 2);return 0;#makefile:ctest:ctest.ogcc ctest.o -lsthc -o ctestctest.o:ctest.cgcc -c ctest.c -o ctest.oall:ctestclean:rm -f *.o ctest1.2用c语言动态方式调用动态库libsthc.so:/*cdltest.c*/#include stdio.h#include stdlib.h#include dlfcn.hint main(void)void *handle;int (*fcn)(int x, int y);const char *errmsg;/* open the library */handle = dlopen(libsthc.so, RTLD_NOW);if(handle = NULL)fprintf(stderr, Failed to load libsthc.so: %sn, dlerror();return 1;dlerror();/*(void *)(&fcn) = dlsym(handle, add);/okfcn = dlsym(handle, add);/okif(errmsg = dlerror() != NULL)printf(%sn, errmsg);return 1;printf(%dn, fcn(1, 5);dlclose(handle);return 0;#makefile:cdltest:cdltest.ogcc cdltest.o -ldl -lsthc -o cdltestcdltest.o:cdltest.cgcc -c cdltest.c -o cdltest.oall:cdltestclean:rm -f *.o cdltest1.3用c+静态方式调用动态库libsthc.so:/*cpptest.cc*/#include libsthc.husing namespace std;int main(void)printf(%dn, add(1, 2);return 0;#makefile:cpptest:cpptest.og+ cpptest.o o cpptest -lsthccpptest.o:cpptest.ccg+ -c cpptest.cc -Wno-deprecated -o cpptest.oall:cpptestclean:rm -f *.o cpptest1.4用c+动态方式调用动态库libsthc.so:/*cppdltest.cpp*/#include stdio.h#include stdlib.h#include dlfcn.hint main(void)void *handle;int (*fcn)(int x, int y);const char *errmsg;/* open the library */handle = dlopen(libsthc.so, RTLD_NOW);if(handle = NULL)fprintf(stderr, Failed to load libsthc.so: %sn, dlerror();return 1;dlerror();*(void *)(&fcn) = dlsym(handle, add);/ok/fcn = dlsym(handle, add);/not ok in c+if(errmsg = dlerror() != NULL)printf(%sn, errmsg);return 1;printf(%dn, fcn(1, 5);dlclose(handle);return 0;#makefilecppdltest:cppdltest.og+ cppdltest.o -ldl -lsthc -o cppdltestcppdltest.o:cppdltest.cppg+ -c cppdltest.cpp -o cppdltest.oall:cppdltestclean:rm -f *.o cppdltest2.用c+语言写动态库:/* libsthcpp.h* Declarations for function cppadd*/#include stdio.h#include stdlib.h#include stdarg.h#ifdef _cplusplusextern C#endifint cppadd(int x, int y);#ifdef _cplusplus#endif/* libsthcpp.cpp* Implementation of function cppadd declared in libsthcpp.h* in c+ language*/#include libsthcpp.hint cppadd(int x, int y)return x + y;#makefilelibsthcpp.so:libsthcpp.og+ -g -shared -Wl libsthcpp.o -lc -o libsthcpp.solibsthcpp.o:libsthcpp.cc libsthcpp.hg+ -g -fPIC -c libsthcpp.cc -o libsthcpp.oall:libsthcpp.soclean:rm -f *.o *.somake完成后,会生成一个动态库,即libsthcpp.so。为了使其他程序也可以使用该动态库,需要将库文件libsthcpp.so拷贝到/usr/lib目录下(由于权限的问题,一般要以root的身分进行拷贝),为了使其他程序也可以使用该动态库,需要将头文件libsthcpp.h拷贝到/usr/include目录下(由于权限的问题,一般要以root的身分进行拷贝)。2.1用c语言静态方式调用动态库libsthcpp.so:/* ctest.c* Testing program for libsthcpp.so library* in c languange* by玄机逸士*/#include libsthcpp.hint main(void)printf(%dn, cppadd(1, 2);return 0;#makefilectest:ctest.ogcc ctest.o -lsthcpp -o ctestctest.o:ctest.cgcc -c ctest.c -o ctest.oall:ctestclean:rm -f *.o ctest2.2用c语言动态方式调用动态库libsthcpp.so:/*cdltest.c*/#include stdio.h#include stdlib.h#include dlfcn.hint main(void)void *handle;int (*fcn)(int x, int y);const char *errmsg;/* open the library */handle = dlopen(libsthcpp.so, RTLD_NOW);if(handle = NULL)fprintf(stderr, Failed to load libsthc.so: %sn, dlerror();return 1;dlerror();/*(void *)(&fcn) = dlsym(handle, cppadd);/ok in c and c+fcn = dlsym(handle, cppadd);/ok in c, but not in c+if(errmsg = dlerror() != NULL)printf(%sn, errmsg);return 1;printf(%dn, fcn(1, 5);dlclose(handle);return 0;#makefilecdltest:cdltest.ogcc cdltest.o -ldl -lsthcpp -o cdltestcdltest.o:cdltest.cgcc -c cdltest.c -o cdltest.oall:cdltestclean:rm -f *.o cdltest2.3用c+语言静态方式调用动态库libsthcpp.so:/* cpptest.cpp* Testing program for libsthc.so library written in c language* in c+ languange* by玄机逸士*/#include libsthcpp.h#include iostream.hint main(void)cout cppadd(1, 2) endl;return 0;#makefilecpptest:cpptest.og+ cpptest.o -lsthcpp -o cpptestcpptest.o:cpptest.cppg+ -c cpptest.cpp -Wno-deprecated -o cpptest.oall:cpptestclean:rm -f *.o cpptest2.4用c+语言动态方式调用动态库libsthcpp.so:/*cppdltest.cpp*/#include stdio.h#include stdlib.h#include dlfcn.hint main(void)void *handle;int (*fcn)(int x, int y);const char *errmsg;/* open the library */handle = dlopen(libsthcpp.so, RTLD_NOW);if(handle = NULL)fprintf(stderr, Failed to load libsthc.so: %sn, dlerror();return 1;dlerror();*(void *)(&fcn) = dlsym(handle, cppadd);/ok in c and c+/fcn = dlsym(handle, cppadd);/ok in c, but not in c+if(errmsg

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论