1

I apologize for the simplicity of this question, but my background is Visual Studio C++ development on Windows and I'm trying to work out how C++ development in Linux works. I'm a bit confused by GCC versions and libc versions.

In Windows you generally have versions of the C standard library that are coupled to compiler versions. For example, if I build a program with VS2013 then I need to redistribute a specific C runtime DLL with my application; e.g. msvcr120.dll

I'm trying to figure out how this works in the Linux world. I installed CentOS 7 with dev tools, and created a simple hello world app. GCC 4.8.5 is installed by default, so I used that and then did 'ldd foo' on my app. ldd prints out /lib64/libc.so.6, and if I inspect libc.so.6 I see it's version 2.17 and compiled with GCC CC 4.8.5. This all makes complete sense.

I get confused when I install and use devtoolset-7, as per the instructions at: https://www.softwarecollections.org/en/scls/rhscl/devtoolset-7/

After install, "gcc --version" tells me I have GCC 7.3.1, and that binary is in /opt/rh/devtoolset-7/root/usr/bin/gcc. This is clear. What's not clear is when I do 'ldd foo' on my new binary, I still see /lib64/libc.so.6, and upon inspection it's still version 2.17 built with GCC 4.8.5. My program still runs, but I have no idea how it's working. Wouldn't libc.so.6 have a different ABI from GCC 4.8.5 than GCC 7.3.1? Coming from the Windows world I would expect to see another version of libc.so.n that GCC 7.3.1 would be using.

I get even more confused by libstdc++.so.6. Surely GCC 4.8.5 and 7.3.1 have different implementations of C++11 and beyond. How does this work?

Thanks for any information that can help me learn how the C runtime works with different compiler versions on Linux.

BigHands79
  • 229
  • 4
  • 18
  • 1
    Are you planning to distribute compiled binaries to others who might not be running CentOS 7? If so you've probably got more to worry about; if you're just going to compile and run on the same machine then everything should just work OK. – Rup Apr 01 '19 at 16:40
  • 1
    Does your GCC 7.3 definitely use the same libstdc++ as GCC 4.8.5? – Rup Apr 01 '19 at 16:43
  • Linux takes forwards/backwards compatibility far more seriously than Microsoft, so `libc.so.6` has been the standard C dynamic library for decades and it uses _symbol versioning_ if needed to provide old implementations. – Iwillnotexist Idonotexist Apr 01 '19 at 16:43
  • @Rup: No I'm not. I'm only targeting CentOS 7. – BigHands79 Apr 01 '19 at 16:46
  • @Rup: As far as I can tell GCC 7.3 is using the same libstdc++ as GCC 4.8.5. ldd tells me it's '/lib64/libstdc++.so.6'. There is a libstdc++.a under devtooset-7 as well as libstdc++.so, but that file is only 210 bytes. I'm not statically linking, so my executable should be loading /lib64/libstdc++.so.6 – BigHands79 Apr 01 '19 at 16:52
  • @IwillnotexistIdonotexist "Linux takes forwards/backwards compatibility far more seriously than Microsoft" - Really? Based on what? Windows 10 even ships with msvcrt.dll which is 20+ years of runtime library support. – Rup Apr 01 '19 at 17:07
  • I see a 4+MB libstdc++.a under /opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7, and no .so. I'd guess that it only supports statically linking libstdc++ to avoid API differences between C++ code built using it and the system compiler, but I've never come across devtoolset before. – Rup Apr 01 '19 at 17:11
  • @Rup Glibc also has 20+ years of runtime library support, as the same file uses ELF symbol versioning to provide old implementations to software compiled against old versions of the C library. More on that [here](https://stackoverflow.com/questions/6495817/why-glibc-binary-is-called-libc-so-6-not-a-libc-so-1-or-libc-so-4). – Iwillnotexist Idonotexist Apr 01 '19 at 17:17
  • 1
    @Maxim: Thank you for posting that link. The key sentence in that post that sorted things out for me is from Jonathan Wakely: "The pieces of the newer C++ runtime from GCC 6.4.0 get statically linked into your binary, and at runtime it only depends on the old libstdc++.so which every CentOS system has installed." – BigHands79 Apr 01 '19 at 18:06
  • 1
    @BigHands79 Yep, that is the crux of it. – Maxim Egorushkin Apr 01 '19 at 18:07

0 Answers0