简体   繁体   English

Ubuntu 18.04 上的 GCC > 7,预计可以工作?

[英]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 时,幕后发生了什么。

  1. Starting with a plain Ubuntu 18.04, I have:从普通的 Ubuntu 18.04 开始,我有:
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25
  1. Then I install gcc-11 (via the toolchains/test ppa repo) and I get:然后我安装 gcc-11(通过工具链/test ppa repo),我得到:
/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

  1. I can compile a hello-world application with 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:我的问题是:

  • Is this expected to work or I'm just lucky with my tiny example?这是预期的工作还是我的小例子很幸运? What can possibly go wrong?什么可能出错?
  • Why is the system-provided library overriden?为什么系统提供的库会被覆盖? With what?什么?
  • If there's a build of GCC-11 for Ubuntu 18.04, does it mean it's guaranteed to work in Ubuntu 18.04?如果有适用于 Ubuntu 18.04 的 GCC-11 版本,是否意味着它可以保证在 Ubuntu 18.04 中工作?
  • In what way do the two libstdc++ libraries (system-provided and gcc-11-provided) differ?两个 libstdc++ 库(系统提供的和 gcc-11 提供的)有何不同?
  • What about other libraries, like libgcc_s.so?其他库呢,比如 libgcc_s.so? The same happens, there's the "system" provided one, and the "GCC-provided" one.同样发生,有“系统”提供的一个,和“海湾合作委员会提供的”一个。
  • Do I need to worry about the remaining libs that are present in ldd?我是否需要担心 ldd 中存在的剩余库? (libc, libm, linux-vsdo). (libc、libm、linux-vsdo)。 It seems there's only one version in the system, but I wonder if they get overriden when installing GCC.系统中似乎只有一个版本,但我想知道在安装 GCC 时它们是否会被覆盖。

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 开始。

    • Copy the example code fromhere .这里复制示例代码。

    • 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)
  • When you install a new GCC on Ubuntu, it will override the system-wide libstdc++ (possibly other libs?) with the GCC-provided 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM