fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
dlerror
(); /* Clear any existing error */
/* Writing: cosine = (double (*)(double
)) dlsym(handle,
"cos");
would seem more natural, but the C99 standard leaves
casting from "void *" to a function pointer undefined.
The assignment used below is the POSIX.1-2003 (Technical
Corrigendum 1
) workaround; see the Rationale for the
POSIX specification of dlsym
()。 */
*(void **
) (&cosine
) = dlsym(handle, "cos");
if ((error = dlerror
()) != NULL
) {
fprintf(stderr, "%s\n", error);
exit(EXIT_FAILURE);
}
printf("%f\n", (*cosine)(0.0));
dlclose(handle);
exit(EXIT_SUCCESS);
}
编译使用库 libdl
gcc t_dlopen.c -ldl
输出
1.000000