Linux操作系统下动态库的编写与调用
来源:互联网 发布:淘宝美工面试题 编辑:程序博客网 时间:2024/04/30 12:00
转自:http://blog.csdn.net/pathuang68/archive/2009/06/16/4273904.aspx
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 << cppadd(1, 2) << endl;
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
- Linux操作系统下动态库的编写与调用
- Linux操作系统下动态库的编写与调用
- Linux操作系统下动态库的编写与调用
- Linux操作系统下动态库的编写与调用
- Linux操作系统下动态库的编写与调用
- Linux动态库的编写与调用
- linux下动态库的编写和调用
- linux下动态链接问题(.so文件的编写与调用)
- linux下动态链接问题(so文件的编写与调用)
- Linux下动态链接库的编写
- Linux下动态链接库的编写
- linux 下的C动态库编写
- Linux下c++调用自己编写的matlab函数:通过mcc动态链接库.so实现
- linux下C语言编程动态库so的编写及调用
- Linux下c++调用自己编写的matlab函数:通过mcc动态链接库.so实现
- Ubuntu环境下使用Unity3D调用C++编写的Linux so动态链接库
- linux操作系统下编译静态库与动态库
- 操作系统的编写与linux的学习
- 多文档菜单
- 全面认识UML类图元素
- flash 游戏设计笔记:递归清除bitmapData
- 在asp.net中下载文件时出现不明的未将对象引用到实例的异常
- spring3种创建对象的方式
- Linux操作系统下动态库的编写与调用
- WEB负载均衡实现方法学习小结
- SQL常用查询语句
- Templating field controls in MOSS page layouts(利用模板开发呈现控件)转http://webarj.wordpress.com/2007/05/07/templating-field-controls-in-m
- 读program_think生产者消费者模式有感
- Android AIDL 理解及开发要点
- 硬链接和软连接的区别
- java获取域账号下的用户完整版
- 快速删除某个用户下的所有表数据