Static linking of Glibc

后端 未结 2 1708
你的背包
你的背包 2020-12-31 06:40

How can i compile my app linking statically glibc library, but only the code needed for my app? (Not all lib)

Now my compile command:

g++  -o newserv         


        
2条回答
  •  无人及你
    2020-12-31 06:48

    Add -static to the compile line. It will only add what your application needs [and of course, any functions the functions you application calls, and any functions those functions call, including a bunch of startup code and some other bits and pieces], so it will be around 800K (for a simple "hello world" program) on an x86 machine. Other architectures vary. Since boost probably also calls the standard library at least a little bit, it's likely that you will have more than 800K added to your appliciation. But it only applies functions used by any of the code in the final binary, not the entire library [about 2MB as a shared library].

    If you ONLY want link glibc, you will need to modify the linking line to your compile to: -Wl,-Bstatic -libc -Wl,-Bdynamic. This will prevent any other library from being linked statically [you sometimes need to have more than one of these statements, as sometimes something pulled in by another library requires "more" from glibc to be pulled in - don't worry, it won't bring in anything more than the linker thinks is necessary].

提交回复
热议问题