http://blog.sina.com.cn/s/blog_602f877001011t8y.html

 

0. -static 作用:
gcc man page中,关于-static的解释如下:

-static
On systems that support dynamic linking, this prevents linking with the shared libraries.  On other systems, this option has no effect.
因为在GCC中,会优先使用shard library. 为了确保使用的是静态库,则使用此选项。

1. 遇到的各类问题:

问题一:
很多第三方程序为了确保在没有相应动态库时运行正常,喜欢在编译最后应用程序时加入-static.
在老版本Fedora中编译正常,但在新版Fedora下编译常常报错:
cannot find -lc 
原因通常是:Fedora下,
#yum install glibc-devel
#yum install glibc
#yum install gcc-c++
时,都不会安装libc.a. 只安装libc.so. 所以当使用-static时,libc.so不能使用。只能报找不到libc了。

解决方法:
yum install glibc-static



问题二:
当test.c中用到math库中函数。
gcc -o test -lm  -static  test.o
此时会报错:
test.c:(.text+0x39): undefined reference to `sqrt'
说是符号找不到。

分析:
因为编译选项中有-static, 所以 -lm 指的是libm.a

这就涉及到符号空穴的实现问题。
Sam猜测:因为编译器从左向右读取文件, 当读到-lm时,因为是静态的,但此时还不知道该把哪个符号抽出来。所以只好往后继续处理,等在main.o中发现
*UND*  00000000 sqrt, 也就是这个符号需要填入时,后面的文件却没有这个符号的实现。所以就抱错了。


但如果去掉 -static. 则编译器编译出的test中此符号还是未定义。
00000000       F *UND*  00000067              sqrt@@GLIBC_2.0
但会在运行时从libm.so中将符号找到。

解决方法:
呵呵,既然编译器只会向后找符号,那很简单,把-lm放后面就行了。
gcc -o test   -static  test.o -lm
编译成功。
且080482d0  w    F .text  00000054 sqrt,看到了吧,符号被填入了。