- 论坛徽章:
- 0
|
程序1 s.cpp
extern "C"
{
int soTest(int a,int b) ;
}
int soTest(int a,int b)
{
return a+b;
}
编译:
g++ -c s.cpp
g++ -shared -o s.so s.o
程序2 t.cpp
#include stdio.h>
#include stdlib.h>
#include dlfcn.h>
int main(int argc, char **argv)
{
void *handle;
int (*soTest)(int,int);
char *error;
handle = dlopen ("./s.so", RTLD_LAZY);
if (!handle)
{
fprintf (stderr, "%s\n", dlerror());
exit(1);
}
(void *)soTest = (int (*)(int,int))dlsym(handle, "soTest");
if ((error = dlerror()) != NULL)
{
fprintf (stderr, "%s\n", error);
exit(1);
}
printf ("%d\n", (*soTest)(2,3));
dlclose(handle);
return 0;
}
编译:
g++ -c t.cpp
g++ -o t t.o -ldl
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/106726/showart_2158997.html |
|