Compiling with -static-libgcc -static-libstdc++ still results in dynamic dependency on libc.so

前端 未结 2 1706
一生所求
一生所求 2020-11-29 02:40

I\'m trying to make an executable that\'s as portable as possible. After removing a few dependencies, I came across the following when running the binary on another system:<

相关标签:
2条回答
  • 2020-11-29 02:52

    First be aware that static linking of libc might not improve portability of your program, as libc might depend on other parts of your system e.g. kernel version.

    If you want to try complete static linking just using -static should the trick. Provided that there are static versions of all used libraries installed.

    You can check if your program has only linked static libraries by using:

    ldd binary_name
    

    EDIT:

    Another way that provides useful information for debugging this problem would be to add --verbose to your linker flags.

    0 讨论(0)
  • 2020-11-29 02:54

    GNU libc is not designed to be statically linked. Important functions, e.g. gethostbyname and iconv, will malfunction or not work at all in a static binary. Arguably even worse, under some conditions a static binary will attempt to dynamically open and use libc.so.6, even though the whole point of static linkage is to avoid such dependencies.

    You should compile your program against uClibc or musl libc instead.

    (This has been true for at least 15 years.)

    0 讨论(0)
提交回复
热议问题