如何用GCC静态链接到CRT?

发布于 2024-08-02 08:31:04 字数 53 浏览 5 评论 0原文

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

How could one link statically to the CRT on Windows / macOS and Linux using GCC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身泪倾城 2024-08-09 08:31:04

只需使用链接器选项-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$

Just use the linker option -static as shown below.

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$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文