我的应用程序受到相当严重的数据丢失方式的旧版本libstdc ++中的错误的影响。如何使用或选择正确的库版本的补救措施是已知的,但对部署和构建的更改不可靠。在被自己咬了不止一次之后,我想停止痛苦,并引入运行时检查以获取足够新版本的libstdc ++。在部署无法使用正确版本的情况下,如何访问该版本以显示较大的警告消息。请注意,我需要的次要版本又名随GCC 8。-rpathLD_LIBRARY_PATHlibstdc++.so.6.0.25GLIBCXX_3.4.25
这是一个Linux程序,仅列出它已加载的DSO的绝对真实路径(如dl_iterate_phdr所列举),这些路径是可访问的文件。(所有linux程序load linux-vdso.so,实际上不是文件)。
main.cpp
#include <link.h>
#include <climits>
#include <cstdlib>
#include <string>
#include <vector>
#include <iostream>
int
get_next_SO_path(dl_phdr_info *info, size_t, void *p_SO_list)
{
auto & SO_list =
*static_cast<std::vector<std::string> *>(p_SO_list);
auto p_SO_path = realpath(info->dlpi_name,NULL);
if (p_SO_path) {
SO_list.emplace_back(p_SO_path);
free(p_SO_path);
}
return 0;
}
std::vector<std::string>
get_SO_realpaths()
{
std::vector<std::string> SO_paths;
dl_iterate_phdr(get_next_SO_path, &SO_paths);
return SO_paths;
}
int main()
{
auto SO_paths = get_SO_realpaths();
for (auto const & SO_path : SO_paths) {
std::cout << SO_path << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对我来说,运行方式如下:
$ g++ -Wall -Wextra main.cpp && ./a.out
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25
/lib/x86_64-linux-gnu/libgcc_s.so.1
/lib/x86_64-linux-gnu/libc-2.27.so
/lib/x86_64-linux-gnu/libm-2.27.so
/lib/x86_64-linux-gnu/ld-2.27.so
Run Code Online (Sandbox Code Playgroud)
如您所见,将显示完整版本。通过一点文件名解析,您可以从那里获取文件名。get_SO_realpaths在查找任何DSO列表之前,先获取整个DSO列表,libstdc++如果需要的话,可以检测到libstdc++加载多个DSO 的异常可能性。