- 论坛徽章:
- 0
|
是在solaris平台下,用c语言编写的程序,其实我试了一下,在linux下也是一样。可能我说的不明白,就是下面这个意思:
在testso.c中
#include<stdio.h>
int test()
{
printf("asdf\n");
return 0;
}
int exproc(const char *sArg)
{
char szBuf[1024];
test();
memset(szBuf, 0, 1024);
sprintf(szBuf, "%s\n", sArg);
return 0;
}
gcc -c -fPIC testso.c
gcc -shared -W1 -o testso.so testso.o
在test.c中
#include <stdio.h>
#include <stdlib.h>
typedef int (*MYPROC)(const char *arg);
void test()
{
printf("11111111111111\n");
}
int main(int argc, char* argv[])
{
void *hinstLib = 0;
MYPROC proc = NULL;
hinstLib = dlopen("./testso.so", RTLD_LAZY);
if (hinstLib != NULL)
{
proc = (MYPROC)dlsym(hinstLib, "exproc");
proc("abc");
dlclose(hinstLib);
}
else
{
printf("%s\n", dlerror());
}
return 0;
}
gcc -c test.c
gcc -W1 -o test test.o -ldl
./test
输出结果是
11111111111111
abc
而不是
asdf
abc |
|