1

My question specifies the version numbers of my scenario, but I'm interested in a general to the question.

I'd like to use gcc 11 on Alma Linux/Red Hat 8 (identical ABI), which come with gcc 8, in order to use C++20/23 features in my own programs. The programs I compile with it (all userspace applications) would run on the same system.

I'm thinking of compiling gcc11 from source, installing it in /usr/local/gcc11/, and calling it when I need it. I don't want to replace/remove the system gcc, since various tools that support a given distro will be expecting that compiler. I would just call a wrapper script to use gcc11 when I need it.

I expect gcc11 will compile just fine, and so will programs I compile with it. But any non-trivial program tends to link against libc, libm, libdl, libpthread, libgcc_s, libstdc++, and so on. I would be using gcc11 with relatively old versions of these system libraries. These are all system libraries which I've never had to deal with directly.

The situation that worries me is if some new dependency I use was written around libpthread version Z but my system has version X, and despite the ABI being unchanged (which allows my program to link successfully against the system libpthread), the behavior is different due to bugfixes, and I end up with subtle runtime bugs.

Is this a valid worry? Or am I golden as soon as my application compiles/launches successfully?

The only existing discussion on this that I could find ( Is there any issue in upgrading GCC version to other than the ones come with distro? ) is light on info, and the warnings given don't apply to me since I don't intend to touch the system gcc/libs.

  • SUGGESTION: Read this article: [How to install multiple versions of GCC and G++ on Ubuntu 20.04](https://www.fosslinux.com/39386/how-to-install-multiple-versions-of-gcc-and-g-on-ubuntu-20-04.htm), and leverage [update-alternatives](https://linux.die.net/man/8/update-alternatives) – paulsm4 Sep 06 '21 at 20:14
  • 1
    That cannot be answered easily there is a related question here [Is it safe to link C++17, C++14, and C++11 objects](https://stackoverflow.com/questions/46746878). – t.niese Sep 06 '21 at 20:36
  • Successful compiling and linking does not necessarily mean that you are fine. As soon as you pass types between the boundaries of compilation units that have been compiled with different compiler versions or against different library versions you could get problems. I encountered such a problem in two of my own libs both used the same header-only library, but of a different version. And I passed an object of that head-only library between them. The problem was that these versions weren't compatible with each other. – t.niese Sep 06 '21 at 20:46

1 Answers1

0

It depends which features you're compiling against. This article from cppreference.com has more details on which c++ features for each language version are supported by which c++ runtime. https://en.cppreference.com/w/cpp/compiler_support

Since you're talking about compiling with gcc, you're going to want to look at the libstdc++ runtime library. It looks like not even every feature of c++20 is even supported by version 8 of the runtime library. But it is most things.

To work around this you could

  • install the runtime version of libstdc++ you want
  • package and distribute the libstdc++ runtime library along with your code
  • statically link against libstdc++, which I don't recommend

Keep in mind that there is theoretically an option to compile against libstdc++ statically, but you're not guaranteed to get those symbols at runtime depending on your situation. It also makes a difference if you're compiling an application vs a library. Loading a library my cause your libraries symbols to conflict with what's already been loaded (probably by libstdc++). This post does a good job explaining some things to look out for https://stackoverflow.com/a/14082540/1196033.

(Also, Android is weird https://developer.android.com/ndk/guides/cpp-support)

xaviersjs
  • 1,579
  • 1
  • 15
  • 25