c++ - 编译标志 -static 对于 gcc 真正意味着什么

标签 c++ c gcc

例如我这样做了:

${CROSS_COMPILE}gcc -static myinit.c -o myinit

我也是在没有静态的情况下做到这一点的:

${CROSS_COMPILE}gcc  myinit.c -o myinit

在我的情况下没有效果,在两种情况下二进制给出相同的结果。

那么 static 这里的作用是什么?

这是我正在编译的程序:

#include <stdio.h>

int
main ()
{
    printf ("\n");
    printf ("Hello world from %s!\n", __FILE__);
    while (1) { }
    return 0;
}

还有

${CROSS_COMPILE}arm-xilinx-linux-gnueabi-

最佳答案

$ 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 静态链接所有依赖项,因此您的二进制文件可以在没有安装所有这些运行时的计算机上运行

关于c++ - 编译标志 -static 对于 gcc 真正意味着什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24095091/

相关文章:

c++ - 在 linux 中创建 "virtual"OpenGL 上下文

c++ - Qt OpenCV 应用程序无法在 Raspberry Pi 显示屏上运行

c - valgrind 大小 1 的读取无效(realloc 产生错误 "use-after-free condition")

c++ - 编译时未定义对 `__sync_val_compare_and_swap_4' 错误的引用,使用 gcc 4.1.1 和 4.2.0 作为 Sparc v8 目标

c++ - 政策是概念吗?

c++ - 交叉编译 PCL 时的 CMake 和 HAVE_POSIX_MEMALIGN

c - 大数组不会导致堆栈溢出

c - 静态库不需要头?

c++ - 使用正确的编译器标志在每个平台上进行相同的模拟

c - C程序中的段错误(核心转储)错误