【问题标题】:How to Statically Link to the CRT with GCC?如何使用 GCC 静态链接到 CRT?
【发布时间】:2009-08-18 11:43:23
【问题描述】:

如何使用 GCC 静态链接到 Windows / macOS 和 Linux 上的 CRT?

【问题讨论】:

    标签: gcc static mingw mingw-w64 crt


    【解决方案1】:

    只需使用链接器选项-static,如下所示。

    edd@ron:/tmp$ cat helloworld.c
    #include <stdio.h>
    
    int main(void) {
        printf("Hello, world\n");
    }
    edd@ron:/tmp$ gcc -o helloworld.dyn helloworld.c
    edd@ron:/tmp$ gcc -static -o helloworld.static helloworld.c
    edd@ron:/tmp$ ls -l helloworld.*
    -rw-r--r-- 1 edd edd     69 2009-08-18 07:09 helloworld.c
    -rwxr-xr-x 1 edd edd   6667 2009-08-18 07:10 helloworld.dyn
    -rwxr-xr-x 1 edd edd 576348 2009-08-18 07:10 helloworld.static
    edd@ron:/tmp$ ./helloworld.dyn
    Hello, world
    edd@ron:/tmp$ ./helloworld.static
    Hello, world
    edd@ron:/tmp$ ldd helloworld.static
            not a dynamic executable
    edd@ron:/tmp$ ldd helloworld.dyn
            linux-gate.so.1 =>  (0xb7efc000)
            libc.so.6 => /lib/i686/cmov/libc.so.6 (0xb7d83000)
            /lib/ld-linux.so.2 (0xb7efd000)
    edd@ron:/tmp$
    

    【讨论】:

    • 不适用于 Windows 上的 MinGW。 AFAIK,没有办法摆脱对使用 MinGW 的 MSVCRT.DLL 的依赖,尽管我很高兴被证明是错误的。
    • 啊,是的,其他操作系统。好吧,至少我得到了三分之二。
    • 我试图与没有-static 的链接,我在 Win7 上使用 MinGW,它会生成 2 个不同的文件(静态文件大 1.5 Mo)。我根本不是专家,但我认为它工作得很好......
    猜你喜欢
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多