[英]What does compilation flag -static really means for gcc
For example I did this: 例如,我这样做:
${CROSS_COMPILE}gcc -static myinit.c -o myinit
Also I did this without static: 我也没有静态地这样做:
${CROSS_COMPILE}gcc myinit.c -o myinit
There is no effect in my case, in both cases binary gives same result. 在我的情况下没有任何效果,在两种情况下二进制给出的结果相同。
So what is the role of static here? 那么静态在这里的作用是什么?
Here is the program I am compiling: 这是我正在编译的程序:
#include <stdio.h>
int
main ()
{
printf ("\n");
printf ("Hello world from %s!\n", __FILE__);
while (1) { }
return 0;
}
Also 也
${CROSS_COMPILE}
is arm-xilinx-linux-gnueabi-
${CROSS_COMPILE}
是arm-xilinx-linux-gnueabi-
From the gcc man page, it's used to enforce static linking of libraries. 在gcc手册页中,它用于强制执行库的静态链接。 Some systems will always link statically if dynamic linking is not supported. 如果不支持动态链接,则某些系统将始终静态链接。
-static On systems that support dynamic linking, this prevents linking with the shared libraries. -static在支持动态链接的系统上,这可以防止与共享库链接。 On other systems, this option has no effect. 在其他系统上,此选项无效。
This option will not work on Mac OS X unless all libraries (including libgcc.a) have also been compiled with -static. Since neither a static version of libSystem.dylib nor crt0.o are provided, this option is not useful to most people.
$ ldd myinit
linux-vdso.so.1 => (0x00007fff5dbfe000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7ec63ce000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7ec67c0000)
$ ldd myinit_static
not a dynamic executable
$ ll
total 884
drwxrwxr-x 2 jarod jarod 4096 Jun 7 16:00 ./
drwxr-xr-x 38 jarod jarod 4096 Jun 7 15:59 ../
-rwxrwxr-x 1 jarod jarod 8567 Jun 7 16:00 myinit*
-rw-rw-r-- 1 jarod jarod 136 Jun 7 16:00 myinit.c
-rwxrwxr-x 1 jarod jarod 877388 Jun 7 16:00 myinit_static*
-static link all dependency statically, so your binary can run on a machine without all these runtime installed -static静态链接所有依赖项,因此您的二进制文件可以在没有安装所有这些运行时的情况下在计算机上运行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.