【发布时间】:2015-11-13 14:57:16
【问题描述】:
EDIT 发现了一个问题,但仍需要解决它应该在答案下方 我的任务是根据现有文件编写应用程序。 test.c(main) randapi.c randapi.h(这里有2个函数)和initapi.c(一个函数)。 “您如何将动态库用作动态加载的库。使用 eg9(我制作了一个动态库并且效果很好)编写应用程序,该库将动态附加”
这是我对 makefile 的尝试,但终端显示:当我使用 ./program 运行文件时无法打开
我也试过不附加 initapi.c 的版本,但它说 initRand 是未知的,除了使文件清楚地附加它
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#define ITERATIONS 1000000L
int main(int argc, char** argv)
{
long i;
long isum;
float fsum;
void *lib;
lib=dlopen("librandapi.so", RTLD_LAZY);
if (!lib)
{
printf("failed to open");
exit(1);
}
int (*getRand)(int);
float (*getSRand)();
void (*initRand)();
getRand=dlsym(lib,"getRand");
getSRand=dlsym(lib,"getSRand");
initRand=dlsym(lib,"initRand");
initRand();
isum = 0L;
for (i = 0 ; i < ITERATIONS ; i++) {
isum += ((*getRand)(10));
}
printf( "getRand() Average %d\n", (int)(isum / ITERATIONS) );
fsum = 0.0;
for (i = 0 ; i < ITERATIONS ; i++) {
fsum += ((*getSRand)());
}
printf( "getSRand() Average %f\n", (fsum / (float)ITERATIONS) );
dlclose(lib);
return 0;
}
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
makefile
zad9: test.c
gcc -Wall -o zad9 test.c -ldl
librandapi.so: randapi.o initapi.o
gcc -shared -o librandapi.so randapi.o initapi.o
randapi.o: randapi.c randapi.h
gcc -c -Wall -fPIC randapi.c
initapi.o: initapi.c
gcc -c -Wall -fPIC initapi.c
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
zad9: test.c initapi.c
gcc -Wall -o zad9 test.c initapi.c -ldl
librandapi.so: randapi.o initapi.o
gcc -shared -o librandapi.so randapi.o
randapi.o: randapi.c randapi.h
gcc -c -Wall -fPIC randapi.c
【问题讨论】:
-
errno中的值是什么,最好是strerror()翻译,在dlopen()之后?