分类: LINUX
2010-12-04 10:19:22
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