版本定义符号还提供了一种机制,可检验通过 dlopen(3C) 获取的目标文件的版本需求。使用此函数添加到进程地址空间的任何目标文件都不会使运行时链接程序执行自动版本依赖项验证。这样,此函数的调用程序将负责检验是否满足所有版本控制需求。
可以通过使用 dlsym(3C) 查找关联的版本定义符号来检验是否存在所需版本定义。在以下示例中,使用 dlopen(3C) 将共享库 libfoo.so.1 添加到进程中,并检验接口 SUNW_1.2 是否可用。
#include <stdio.h> #include <dlfcn.h> main() { void * handle; const char * file = "libfoo.so.1"; const char * vers = "SUNW_1.2"; .... if ((handle = dlopen(file, (RTLD_LAZY | RTLD_FIRST))) == NULL) { (void) printf("dlopen: %s\n", dlerror()); exit (1); } if (dlsym(handle, vers) == NULL) { (void) printf("fatal: %s: version `%s' not found\n", file, vers); exit (1); } .... |