Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static linking of Glibc

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 newserver  test.cpp ... -lboost_system -lboost_thread -std=c++0x

Thanks!

like image 314
Breakdown Avatar asked Jan 28 '13 17:01

Breakdown


People also ask

What is glibc static?

The glibc-static package contains the C library static libraries for -static linking. You don't need these, unless you link statically, which is highly discouraged.

What is static library linking?

In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.

What does static linking do?

Static linking is the result of the linker copying all library routines used in the program into the executable image. This may require more disk space and memory than dynamic linking, but is both faster and more portable, since it does not require the presence of the library on the system where it is run.

What happens when you link a static library?

Static Linking and Static Libraries is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory.


1 Answers

That's what -static does (as described in another answer): unneeded modules won't get linked into your program. But your expectations on the amount of stuff which is needed (in a sense that we can't convince linker to the contrary) may be too optimistic.

If you trying to do it for portability (running an executable on other machines with older glibc or something like that), there is one easy test question to see if you're going to get what you want:

Did you think of the problem with libnss, and are you sure it is not going to bite you?

If your answer is yes, maybe it makes sense to go on. If the answer is no, or the question seems too obscure and there is no answer, just quit your expirements with statically linked glibc: it has more chance to hurt than help.

like image 174
Anton Kovalenko Avatar answered Oct 24 '22 06:10

Anton Kovalenko