如何使用GCC静态链接到CRT?

问题描述 投票:0回答:1

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

gcc static mingw mingw-w64 crt
1个回答
2
投票

只需使用链接器选项-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$
© www.soinside.com 2019 - 2024. All rights reserved.