gcc -static:
表示的是编译的时候禁止调用动态库,此时就是说完全编译出的东西是比较大的。让 gcc 进行静态编译,也就是把所有都需要的函数库都集成进编译出来的程序上,这个程序就可以不依赖外部的函数库运行了。
- -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. 为了确保使用的是静态库,则使用此选项。 - 遇到的各类问题:
问题一:
很多第三方程序为了确保在没有相应动态库时运行正常,喜欢在编译最后应用程序时加入-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:(.