简体   繁体   中英

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-

From the gcc man page, it's used to enforce static linking of libraries. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM