[英]GCC > 7 on Ubuntu 18.04, expected to work?
I want to understand what's happening under the hood when using a newer GCC than the "default" version for a given version of Ubuntu.我想了解在使用比给定 Ubuntu 版本的“默认”版本更新的 GCC 时,幕后发生了什么。
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25
/usr/lib/gcc/x86_64-linux-gnu/11/libstdc++.so
I also notice that the system-provided version gets overriden!我还注意到系统提供的版本被覆盖了!
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.29
g++-11
, and get the following via ldd
:我可以用g++-11
编译一个 hello-world 应用程序,并通过ldd
获得以下内容:ldd a.out
linux-vdso.so.1 (0x00007ffc79ff7000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fd378546000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd378155000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fd377db7000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd378b55000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fd377b9f000)
So it's linking to the system-installed version of libstdc++
, instead of the compiler-provided version.所以它链接到系统安装的libstdc++
版本,而不是编译器提供的版本。 The binary runs just fine.二进制运行得很好。
My questions are:我的问题是:
Thanks!谢谢!
After all the great info I got from the comments, I think I got a good enough understanding to answer the question.在我从评论中获得了所有重要信息之后,我想我已经有了足够的理解来回答这个问题。 Thanks everyone!谢谢大家!
If the libstdc++
has the same major version (the one in the SONAME), then they are backwards-compatible .如果libstdc++
具有相同的主要版本(SONAME 中的版本),则它们是向后兼容的。 Meaning that something built on a older libstdc++
is guaranteed to run on a newer libstdc++
.这意味着建立在较旧的libstdc++
上的东西保证可以在较新的libstdc++
上运行。
The opposite is not true in general - the library is not forward-compatible .一般情况下,情况并非如此——库不是向前兼容的。 If a new symbol is introduced in a new library, it naturally doesn't exist in the older library.如果在新库中引入了新符号,则旧库中自然不存在该符号。 This can be reproduced as follows:这可以重现如下:
Start on Ubuntu 20.04.从 Ubuntu 20.04 开始。
Compile it with the default GCC 9 and -std=c++17
.使用默认的 GCC 9 和-std=c++17
编译它。
This binary runs fine on Ubuntu 20.04.这个二进制文件在 Ubuntu 20.04 上运行良好。
Now, copy that binary over to a stock Ubuntu 18.04.现在,将该二进制文件复制到 Ubuntu 18.04。
You'll get a "symbol not found error":你会得到一个“找不到符号的错误”:
./a.out: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.26' not found (required by ./a.out)
libstdc++
(so they are identical).当您在 Ubuntu 上安装新的 GCC 时,它将使用 GCC 提供的libstdc++
(因此它们是相同的)覆盖系统范围的 libstdc++(可能是其他库?)。 This will ensure that whatever you build on that machine will also run there.这将确保您在该机器上构建的任何内容也将在那里运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.