cmakestaticg++glibc

compile shared library that contains low-level static library


I have a tool that is compiled on Ubuntu 22.04, but this tool needs to run on a Centos 7 system. Ubuntu has a higher version of glibc and when running the compiled tool on Centos 7, it has error:

ImportError: /lib64/libm.so.6: version `GLIBC_2.27' not found

This error is from a shared library file that is needed in the tool. So I want to include the required low-level library into my tool. I do have libm.a file in the compile system and I want to merge it into the shared library.

I tried to merge it with command like:

  find_library(M_LIBRARY m REQUIRED)
  find_library(Z_LIBRARY z REQUIRED)
  target_link_options(onnx_cpp2py_export PRIVATE "-Wl,-Bstatic" "-lm" "-lc" "-lz")

and

target_link_libraries(onnx_proto PRIVATE "/usr/lib/x86_64-linux-gnu/libz.a"
                                         "/usr/lib/x86_64-linux-gnu/libm.a"
                                         "/usr/lib/x86_64-linux-gnu/libc.a"
                                         "-Wl,-fPIC")

and neither worked. I say it did not work based on the fact that onnx_cpp2py_export.so still repy on libm.so.6

root@75b3e00f1508:/static/build# ldd onnx_cpp2py_export.so
        linux-vdso.so.1 (0x00007ffeae184000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f68d55fd000)
        libm.so.6 => /usr/lib/x86_64-linux-gnu/libm.so.6 (0x00007f68d5516000)
        libgcc_s.so.1 => /usr/lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f68d54f6000)
        libc.so.6 => /usr/lib/x86_64-linux-gnu/libc.so.6 (0x00007f68d52cd000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f68d5fb9000)

What should I do here?


Solution

  • So I want to include the required low-level library into my tool. I do have libm.a file in the compile system and I want to merge it into the shared library.

    This approach would generally not work, because libm.a has dependencies on libc.so, and you can not mix and match multiple parts of GLIBC in a single process.

    If will also not work to simply use libm.a from either system, because libm.a is normally not built with -fPIC, and thus can't be linked into a shared library on x86_64.


    If your tool needs to run CentOS 7, your best (and the only guaranteed to work) solution is to link it against CentOS 7 compatible libraries.

    The easiest way to do is to set up a docker container with CentOS 7, and build your tool in that container.

    Other alternatives include building in a chroot, in a VM, or using a Linux-to-older-Linux crosscompiler. But all of these are more involved than a simple docker container.