Search code examples
c++build

How to build C++17 application in old linux distro with old stdlib and libc?


I need to build a C++17 application with distro having libstdc++ 6.0.22 (GLIBCXX 3.4.22) and libc 2.24. These versions of libraries don't allow me to build modern c++ and I cannot update system's versions. In fact, I need a correct working application (may be carrying compiled stdlib from sources with it) that I could give to my clients and they would work with it. The distro has llvm-9 in its repos and that provides a standard library I could use

I tried 2 things:

  1. To build the app against libc++ but that will force me to recompile system dependencies too and distribute them with my package as well (but i don't even have an idea how to do that)
  2. To build libstdc++ from sources and distribute it with my package

The (1) approach took too much time to compile and I need to know how do I recompile system dependencies and distribute them. It is not a small hello world application, it uses a lot of system deps
The (2) approach I just tried and failed because of a lot of errors (unknown type, unexpected identifier, etc..)


Solution

  • libstdc++ is a part of the C++ compiler (gcc or llvm). You need not only the current libstdc++ but also the compiler that goes with it. The current version of each compiler (which is needed for support C++17) requires the current version of libstdc++ for programs that are compiled with it. I haven't double-checked the binutils dependencies, but the newer compilers will likely require a newer version of binutils (and, specifically, the ld linker). The advanced features of the new C++ standards likely required new functionality implemented in the system linker. Not to mention the new compiler functionality as well (link-time optimizations in the curren version of gcc, for example, require closely tied support from the linker, ld, or gold, which will actually invoke the compiler again during the link phase).

    Your only realistic solution is to bootstrap a secondary toolchain, starting with binutils, and then finally with the current version of your compiler. You do not have have to update or replace your system-installed libstdc++, or compiler, or ld. By running their respective configuration scripts it's possible to install the entire toolchain in a separate directory hierarchy so that they will not interfere with your system-installed binutils and compiler.

    As far as other system dependencies go, you probably need to rebuild only the C++ ones. System dependencies that are C libraries are unlikely to need a rebuild. Still, if you have some, you will have to rebuild them, that's just the way it is, there are no alternative shortcuts.

    That's pretty much it. There are no other alternatives. As you can surmise, bootstrapping an entire toolchain in order to "side-load it" onto a system with an existing compiler toolchain, in a manner as to not interfere with it, is not a small endeavour. It's a challenge even for skilled, experienced developers or system administrators. But this is certainly doable, I've done this a few years ago, in a previous job. It was possible to do that, so I see no reason why it's not possible to do it now; and this is pretty much the only feasible way to be able to support a modern version of C++ on a distribution that comes with an older compiler tolchain (that does not support the current C++ standard).