-static option for gcc?

懵懂的女人 提交于 2019-12-17 10:58:33

问题


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

What needs installation?

GCC version:

[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) 

回答1:


I'm wondering what the -static option on gcc does.

Just pass (in addition) the -v flag to gcc to find out. And please read about GCC command options and later the binutils documentation, notably the documentation of ld.

assuming a Linux system

If you don't have a Linux system, this entire answer is irrelevant

I strongly recommend taking several hours to read Drepper's: How To Write Shared Libraries paper. It explain in details what the dynamic linker is actually doing. And you need to understand that to decide when you don't want to use it, that is when you really want a static linking. My strong opinion is that there are very few cases for that, but when you really need static linking (e.g. for sash) you just cannot avoid it.





You generally should avoid statically linking your application (and you should explain us why you don't want usual dynamic linking). I suggest at least to link dynamically the system libraries (libc notably) - if you absolutely want, you could link less common libraries statically - . Why do you want to link statically your application? It is generally a mistake (because you don't profit from updates to the system dynamic libraries). In particular name service switch facilities from libc wants dynamic libraries.

Your system should have installed the package providing the static libc library. On Debian, it is the libc-dev package but I don't know what it is on RedHat.

To find out what gcc does, pass it the -v flag like

  gcc -v -static -O3 -o prog prog.c

But you should not link statically your programs without good reasons. On my Debian distributions, there are more than 700 programs in /usr/bin and only one is statically linked.

I'm wondering what the -static option on gcc does.

As to what the -static option of GCC does, read on Invoking GCC. It is a linking option which:

-static

On systems that support dynamic linking, this overrides -pie and prevents linking with the shared libraries. On other systems, this option has no effect.

The -pie flag means:

-pie

Produce a dynamically linked position independent executable on targets that support it. For predictable results, you must also specify the same set of options used for compilation (-fpie, -fPIE, or model suboptions) when you specify this linker option.

The /usr/bin/ld: cannot find -lc message is surprising, and might reveal other problems (badly installed or corrupted system, or some missing libc.a).




回答2:


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.

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...

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.

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.




回答3:


The -static flag forces the linker to accept only static libraries and not any shared libraries.

If you want to use -static, you have to ensure that you have a static version of the C library installed, which might be tricky to find (most systems do not have a static C library any more). Or you have to cancel the effect of -static. However, in the example, that would defeat the purpose of -static since the only library linked is (implicitly) the C library.



来源:https://stackoverflow.com/questions/8692128/static-option-for-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!