关于gcc-static的解释:因为在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 ,既然编译器只会向后找符号,那很简单,把-lm放后面就行了。gcc -o test -static test.o -lm编译成功。且080482d0 w F .text 00000054 sqrt,符号被填入了。gcc man page中,关于-static的解释如下:-staticOn systems that support dynamic linking, this prevents linking wit`sqrt’说是符号找不到。分析因为编译选项中有-static, 所以 -lm 指的是libm.a这就涉及到符号空穴的实现问题。因为编译器从左向右读取文件, 当读到- lm时,因为是静态的,但此时还不知道该把哪个符号抽出来。所以只好往后继续处理,等在main.o中发现UND 00000000 sqrt, 也就是这个符号需要填入时,后面的文件却没有这个符号的实现。所以就抱错了。但如果去掉 -static. 则编译器编译出的test中此符号还是未定义,但会在运行时从libm.so中将符号找到。解决方法:呵呵,既然编译器只会向后找符号,那很简单,把-lm放后面就行了。gcc -o test -static test.o -lm编译成功。
对于gcc—static作用的认识
最新推荐文章于 2023-07-05 10:33:17 发布