1

What other stuff does -static add to the final binary output that -static-libstdc++ does not add?

I checked the excellent answer for this question but it doesn't address this particular question that I have.

I tried the following program:

#include <iostream>


int main( )
{
    std::cout << "What does -static do that -static-libstdc++ doesn't do?\n";
}

Without any of these options specified for link-time, the generated output is only ~17 KB. However, by specifying -static-libstdc++ it becomes ~1.3 MB. And if -static is used instead, it becomes ~2.4 MB. What is being added by the -static flag that causes the latter two forms to have such a huge difference in size? And which things can be affected at runtime if only the -static-libstdc++ is specified at the linking stage?

Now which one is more suitable if someone wants to build a small program that can be run on Ubuntu (v18.04 and later)?

digito_evo
  • 3,216
  • 2
  • 14
  • 42
  • All the other libraries which are not libstdc++, e.g. the C standard library. – user17732522 Apr 16 '22 at 18:31
  • @user17732522 And the other libraries from the Linux itself? – digito_evo Apr 16 '22 at 18:36
  • In addition to the C standard library (libc.a and potentially some others) maybe also some of runtime libraries of the compiler (libgcc.a or similar). The linux kernel itself doesn't come with any dynamic/static libraries (except if you want to count vDSO, but that is special anyway). – user17732522 Apr 16 '22 at 18:38
  • @user17732522 So which one should be used if someone wants to make his small program to be used in various versions of e.g. Ubuntu? Does it even make any difference for small C++ programs that only rely on libstdc++? – digito_evo Apr 16 '22 at 18:49
  • If that is your main question, I suggest making it more prominent in your posted question. Also describe the systems you want the executable to run on (e.g. versions of Ubuntu as you mentioned). The answer may be different for different operating systems/distributions. – user17732522 Apr 16 '22 at 18:59
  • @user17732522 Yes have a look. – digito_evo Apr 16 '22 at 19:05
  • 1
    `-static` implies at least `-static-libgcc` and `-static-libstdc++`. If you want a portable program, I wouldn't try static linking (at least since eventually you'll run into a LGPL licensed library, which requires you to either link dynamically or provide your source). Instead try distributing the right libraries with your application, and force them to be used instead of the system ones either with `LD_LIBRARY_PATH` or by changing rpath of your executable (and the libraries, using `patchelf`). – HolyBlackCat Apr 16 '22 at 19:17
  • @HolyBlackCat Yes thanks for the tips. Also, would it be problematic if I link statically and have the source available in a public repo (under GPLv3) on GitHub? – digito_evo Apr 16 '22 at 20:11
  • 1
    @digito_evo That's fine for LGPL. – HolyBlackCat Apr 16 '22 at 20:17

0 Answers0