c++libc

Build a Linux c++ application runnable on system having libc >= 2.31


I would like to build a C++ application which can be launched on all Linux systems having libc >= 2.31. On my build system (Ubuntu), I have libc 2.34. Here is my (empty) application:

int main() {
  return 0;
}

I built it with g++ -o app app.cpp and according to the following result, I understand that my application requires libc >= 2.34 to run. Is my understanding correct ?

$ nm -D app
                 w __cxa_finalize@GLIBC_2.2.5
                 w __gmon_start__
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 U __libc_start_main@GLIBC_2.34

How build my application for libc <= 2.31 ? I tried to add "__asm__ (".symver __libc_start_main, __libc_start_main@GLIBC_2.2.5");" (based on my understanding of this post) before my main method but it does not change anything.


Solution

  • Docker image ubuntu:20.04 has libc 2:31 installed. You could compile your application there:

    $ docker run --rm -v $PWD:/work -w /work ubuntu:20.04 bash -c 'apt-get update && apt-get -y install g++ && g++ -o app app.cpp'
    $ docker run --rm -v $PWD:/work -w /work ubuntu:20.04 ./app
    $ ./app
    $ nm -D app
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 w __cxa_finalize@GLIBC_2.2.5
                 w __gmon_start__
                 U __libc_start_main@GLIBC_2.2.5
    

    You could link your application statically, remove all dependencies on standard library.

    You could distribute your application with libc library together.