《Linux操作系统下动态库的编写与调用.doc》由会员分享,可在线阅读,更多相关《Linux操作系统下动态库的编写与调用.doc(8页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、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, in
2、t 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的身分进行拷贝),为了使其他程序也可以使用该动态
3、库,需要将头文件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 c
4、test.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,
5、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 cdltestc
6、dltest.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 -Wn
7、o-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)f
8、printf(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+ cppdlt
9、est.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);#ifde
10、f _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
11、 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 pro
12、gram 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
13、.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, cppad
14、d);/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:cdltestcl
15、ean: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 cpptestcpp
16、test.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 = dlop
17、en(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;#makefilecppdltest:cppdltest.og+ cppdltest.o -ldl -lsthcpp -o cppdltestcppdltest.o:cppdltest.cppg+ -c cppdltest.cpp -o cppdltest.oall:cppdltestclean:rm -f *.o cppdltest
限制150内