开发者

gcc linking only 'required functions'

开发者 https://www.devze.com 2023-03-14 06:17 出处:网络
Wh开发者_如何学编程ile 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

Wh开发者_如何学编程ile 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号