KawaiKx
KawaiKx

Reputation: 9908

gcc linking only 'required functions'

While compiling a C program, gcc links the standard C library by default. Is it possible to link only the selected functions, say printf instead of the complete C standard library, in an attempt to reduce the size of the executable to a bare minimum?

Upvotes: 3

Views: 2252

Answers (1)

CB Bailey
CB Bailey

Reputation: 791631

With most traditional linkers static library linking is done on an object file basis. gcc will normally use the system linker on the system that you are using.

Traditionally a static library is just an archive file consisting of the object files that form the library. When you link a static library into your program the linker will extract any object files from the library that help resolve any unresolved symbols in your program, including those introduced by object files from the library that helped resolve previously unresolved symbols.

In theory, if the standard library implementation consisted of one object file per function and there were no depedencies between the standard library functions then you would only get just the functions that you explicitly called. In practice, you are likely to get more functions than you explicitly call included.

Dynamic linking is completely different. In this case your program will simply contain a reference to the standard library shared object which will be loaded in its entirety into your processes memory space at run time.

Upvotes: 8

Related Questions