程序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
阅读(681) | 评论(0) | 转发(0) |