问题描述
我想知道 gcc 上的 -static 选项有什么作用.我在编译某个应用程序时需要这个选项,但是当我编译时出现以下错误:
I'm wondering what the -static option on gcc does. I need this option when compiling a certain application, however when I do I get the following error:
gcc -static -O3 -o prog prog.c
/usr/bin/ld: cannot find -lc
collect2: ld returned 1 exit status
需要安装什么?
海湾合作委员会版本:
[user@localhost dir]$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.6.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.6.1 20110908 (Red Hat 4.6.1-9) (GCC)
推荐答案
-static
选项静态链接程序,换句话说,它不需要在运行时依赖动态库来跑.
The -static
option links a program statically, in other words it does not require a dependency on dynamic libraries at runtime in order to run.
要实现静态链接,需要系统上存在库的存档 (.a) 版本.所以/usr/lib/libc.a/usr/lib/crt1.o 等等...
To achieve static linking requires that the archive (.a) versions of your libraries exist on the system. so /usr/lib/libc.a /usr/lib/crt1.o etc...
在现代 linux 系统上(如您使用 red hat):当一个二进制文件将它链接在一起时 1) 要么通过 .o 和 .a 文件将代码放入可执行文件中,要么 2) 放入对动态库 (.so) 由/lib/ld-linux.so(或/lib64/ld-linux=x86-64.so)解析的文件,它总是在一个众所周知的地方.
On modern linux systems (as you are using red hat): when a binary links together it 1) either puts the code into the executable via .o and .a files, or 2) puts in references to dynamic libraries (.so) files that is resolved by /lib/ld-linux.so (or /lib64/ld-linux=x86-64.so) which is always at a well known place.
对于您的特定系统,如果某个程序专门希望创建自身的静态版本,那么您需要安装开发工具的静态版本.您至少需要 glibc-static 包.您可能还需要 libstdc++-static 包.
For your particular system, if a program is specifically looking to create a static version of itself then you need to install the static versions of your devel tools. You need, at the minimum, glibc-static package. You may also need libstdc++-static package as well.
这篇关于-gcc 的静态选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!