免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1345 | 回复: 0
打印 上一主题 下一主题

Linux操作系统下动态库的编写与调用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-24 15:02 |只看该作者 |倒序浏览
1. 用c语言写动态库:
/*
* libsthc.h
* Declarations for function add
*/
#include "stdio.h"
#include "stdlib.h"
#include "stdarg.h"

#ifdef __cplusplus
extern "C"
{
#endif
int add(int x, int y);

#ifdef __cplusplus
}
#endif

/*
* libsthc.c
* Implementation of function add declared in libsthc.h
* in c language
*/
#include "libsthc.h"

int add(int x, int y)
{
         return x + y;
}

#makefile
libsthc.so:libsthc.o
         gcc -shared libsthc.o -lc -o libsthc.so
libsthc.o:libsthc.c libsthc.h
         gcc -fPIC -c libsthc.c -o libsthc.o
all:libsthc.so
clean:
         rm -f *.o *.so

make完成后,会生成一个动态库,即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.h"
int main(void)
{
         printf("%d\n", add(1, 2));
         return 0;
}

#makefile:
ctest:ctest.o
         gcc ctest.o -lsthc -o ctest
ctest.o:ctest.c
         gcc -c ctest.c -o ctest.o
all:ctest
clean:
         rm -f *.o ctest

1.2 用c语言动态方式调用动态库libsthc.so:
/*cdltest.c*/
#include "stdio.h"
#include "stdlib.h"
#include "dlfcn.h"

int 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: %s\n", dlerror());
                   return 1;
         }
         dlerror();

         //*(void **)(&fcn) = dlsym(handle, "add");            //ok
         fcn = dlsym(handle, "add");                                   //ok
         if((errmsg = dlerror()) != NULL)
         {
                   printf("%s\n", errmsg);
                  return 1;
         }
         printf("%d\n", fcn(1, 5));
         
         dlclose(handle);
         return 0;
}

#makefile:
cdltest:cdltest.o
         gcc cdltest.o -ldl -lsthc -o cdltest
cdltest.o:cdltest.c
         gcc -c cdltest.c -o cdltest.o
all:cdltest
clean:
         rm -f *.o cdltest

1.3 用c++静态方式调用动态库libsthc.so:
/*cpptest.cc*/
#include "libsthc.h"
using namespace std;
int main(void)
{
         printf("%d\n", add(1, 2));
         return 0;
}

#makefile:
cpptest:cpptest.o
         g++ cpptest.o –o cpptest -lsthc
cpptest.o:cpptest.cc
         g++ -c cpptest.cc -Wno-deprecated -o cpptest.o
all:cpptest
clean:
         rm -f *.o cpptest

1.4 用c++动态方式调用动态库libsthc.so:
/*cppdltest.cpp*/
#include "stdio.h"
#include "stdlib.h"
#include "dlfcn.h"

int 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: %s\n", dlerror());
                   return 1;
         }
         dlerror();

         *(void **)(&fcn) = dlsym(handle, "add");     //ok
         //fcn = dlsym(handle, "add");                        //not ok in c++
         if((errmsg = dlerror()) != NULL)
         {
                   printf("%s\n", errmsg);
                   return 1;
         }
         printf("%d\n", fcn(1, 5));
         
         dlclose(handle);
         return 0;
}

#makefile
cppdltest:cppdltest.o
         g++ cppdltest.o -ldl -lsthc -o cppdltest
cppdltest.o:cppdltest.cpp
         g++ -c cppdltest.cpp -o cppdltest.o
all:cppdltest
clean:
         rm -f *.o cppdltest


2. 用c++语言写动态库:
/*
* libsthcpp.h
* Declarations for function cppadd
*/
#include "stdio.h"
#include "stdlib.h"
#include "stdarg.h"
#ifdef __cplusplus
extern "C"
{
#endif

int cppadd(int x, int y);
#ifdef __cplusplus
}
#endif


/*
* libsthcpp.cpp
* Implementation of function cppadd declared in libsthcpp.h
* in c++ language
*/
#include "libsthcpp.h"

int cppadd(int x, int y)
{
         return x + y;
}

#makefile
libsthcpp.so:libsthcpp.o
         g++ -g -shared -Wl libsthcpp.o -lc -o libsthcpp.so
libsthcpp.o:libsthcpp.cc libsthcpp.h
         g++ -g -fPIC -c libsthcpp.cc -o libsthcpp.o
all:libsthcpp.so
clean:
         rm -f *.o *.so

make完成后,会生成一个动态库,即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.h"
int main(void)
{
         printf("%d\n", cppadd(1, 2));
         return 0;
}

#makefile
ctest:ctest.o
         gcc ctest.o -lsthcpp -o ctest
ctest.o:ctest.c
         gcc -c ctest.c -o ctest.o
all:ctest
clean:
         rm -f *.o ctest


2.2 用c语言动态方式调用动态库libsthcpp.so:
/*cdltest.c*/
#include "stdio.h"
#include "stdlib.h"
#include "dlfcn.h"

int 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: %s\n", 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("%s\n", errmsg);
                   return 1;
         }
         printf("%d\n", fcn(1, 5));
         
         dlclose(handle);
         return 0;
}

#makefile
cdltest:cdltest.o
         gcc cdltest.o -ldl -lsthcpp -o cdltest
cdltest.o:cdltest.c
         gcc -c cdltest.c -o cdltest.o
all:cdltest
clean:
         rm -f *.o cdltest

2.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.h"
int main(void)
{
         cout
         return 0;
}

#makefile
cpptest:cpptest.o
         g++ cpptest.o -lsthcpp -o cpptest
cpptest.o:cpptest.cpp
         g++ -c cpptest.cpp -Wno-deprecated -o cpptest.o
all:cpptest
clean:
         rm -f *.o cpptest

2.4 用c++语言静态方式调用动态库libsthcpp.so:
/*cppdltest.cpp*/
#include "stdio.h"
#include "stdlib.h"
#include "dlfcn.h"

int 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: %s\n", 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("%s\n", errmsg);
                   return 1;
         }
         printf("%d\n", fcn(1, 5));
         
         dlclose(handle);
         return 0;
}

#makefile
cppdltest:cppdltest.o
         g++ cppdltest.o -ldl -lsthcpp -o cppdltest
cppdltest.o:cppdltest.cpp
         g++ -c cppdltest.cpp -o cppdltest.o
all:cppdltest
clean:
         rm -f *.o cppdltest

本文来自CSDN博客,转载请标明出处:
http://blog.csdn.net/pathuang68/archive/2009/06/16/4273904.aspx


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/106726/showart_2159010.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP