My application is affected by a bug in older versions of libstdc++ in a rather serious data-lossy way. The remedies of how to select the right library version with -rpath
or LD_LIBRARY_PATH
are known, but not reliable to changes in deployment and build. After being bitten more than once myself I would like to stop the suffering and introduce a runtime-check for a sufficiently new version of libstdc++. How can I access the version in order to print a big fat warning message in case of the deployment failing to use the right version. Note that I need the minor version libstdc++.so.6.0.25
a.k.a. GLIBCXX_3.4.25
that comes with gcc 8.
Asked
Active
Viewed 1,158 times
4

Zulan
- 21,896
- 6
- 49
- 109
-
1might be helpful: https://stackoverflow.com/questions/10516041/check-gcc-version-in-runtime – BladeMight Oct 24 '18 at 17:18
-
you can define the some variable with result from `g++ -dumpversion`, and then check for it in code. – BladeMight Oct 24 '18 at 17:22
-
1Unfortunately the gcc version doesn't help. In fact we usually build with a non-default gcc 8.2, but that simply links `libstdc++.so.6` - which also happens to be satisfied by the system `libstdc++.so.6.0.22` coming from system gcc 6.3.0. – Zulan Oct 24 '18 at 17:22
-
You can check which library is being used by `gcc -lstdc++` and later check it by `strings` command + `grep GLIBCXX`, and find the `GLIBCXX_3.4.25` from it. – BladeMight Oct 24 '18 at 17:26
-
@BladeMight That's not a runtime check that the program itself can perform in any easy way. – nos Oct 24 '18 at 17:42
-
3On Linux you can check contents of `/proc/self/maps` from the running process for incorrect libraries. Not trivial but not very hard. – dewaffled Oct 24 '18 at 22:24
-
Also see https://stackoverflow.com/questions/10354636/how-do-you-find-what-version-of-libstdc-library-is-installed-on-your-linux-mac – Mihai8 Oct 27 '18 at 09:23
1 Answers
5
Here is a linux program that simply lists the absolute real paths of the DSOs it has
loaded (as enumerated by dl_iterate_phdr) that are accessible files.
(All linux programs load linux-vdso.so
, which isn't actually a file).
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;
}
Which for me runs like:
$ 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
As you see, the full version appears. With a bit of filename parsing, you
can take it from there. Getting the whole DSO list, as per get_SO_realpaths
,
before looking for any libstdc++
would let you detect, if you want, the freak possibility
of more than one libstdc++
being loaded.

Mike Kinghan
- 55,740
- 12
- 153
- 182
-
I admit this is a more complex solution than I had hoped for, but fairly elegant never the less. – Zulan Oct 30 '18 at 16:54