我们来看个现象:

taoge@localhost Desktop> ar -vt /usr/lib/libc.a | grep printf.o
rw-r--r-- 399/399  28564 Oct 26 00:26 2010 vfprintf.o
rw-r--r-- 399/399    988 Oct 26 00:26 2010 vprintf.o
rw-r--r-- 399/399   1360 Oct 26 00:26 2010 reg-printf.o
rw-r--r-- 399/399    984 Oct 26 00:26 2010 fprintf.o
rw-r--r-- 399/399   1016 Oct 26 00:26 2010 printf.o
rw-r--r-- 399/399    972 Oct 26 00:26 2010 snprintf.o
rw-r--r-- 399/399    988 Oct 26 00:26 2010 sprintf.o
rw-r--r-- 399/399    992 Oct 26 00:26 2010 asprintf.o
rw-r--r-- 399/399    960 Oct 26 00:26 2010 dprintf.o
rw-r--r-- 399/399  27488 Oct 26 00:26 2010 vfwprintf.o
rw-r--r-- 399/399   1440 Oct 26 00:26 2010 fxprintf.o
rw-r--r-- 399/399   1308 Oct 26 00:27 2010 iovsprintf.o
rw-r--r-- 399/399    964 Oct 26 00:27 2010 fwprintf.o
rw-r--r-- 399/399    972 Oct 26 00:27 2010 swprintf.o
rw-r--r-- 399/399    992 Oct 26 00:27 2010 vwprintf.o
rw-r--r-- 399/399    996 Oct 26 00:27 2010 wprintf.o
rw-r--r-- 399/399   2524 Oct 26 00:27 2010 vswprintf.o
rw-r--r-- 399/399   1576 Oct 26 00:27 2010 vasprintf.o
rw-r--r-- 399/399   1356 Oct 26 00:27 2010 iovdprintf.o
rw-r--r-- 399/399   2480 Oct 26 00:27 2010 vsnprintf.o
rw-r--r-- 399/399   3212 Oct 26 00:27 2010 obprintf.o
taoge@localhost Desktop>

        我们看到, 有这么多类似的.o库, 没有糅合在一个整体的.o库中, 这是为什么呢?  

        原因我们其实已经说了, 见http://blog.csdn.net/stpeace/article/details/52886361 《a.out/libTest.so静态链接了libme.a, 为什么存在于libme.a中的符号没有在a.out/libTest.so中出现?》

        耦合越低, 链接到最终可执行文件中的库就越少。 如果放在一个大大的.o中, 那么调用printf的时候, 就把其他类printf函数库(如vfprintf.o、vprintf.o等)都链接进去了, 浪费时间, 浪费空间,  没有必要。

        OK, 本文先说到这里了。




本文转载:CSDN博客