将C代码编译成完全静态链接出错

时间:2023-01-08

浏览:220

作者:佚名

本篇文章给大家分享《将C代码编译成完全静态链接出错》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。

问题内容

正常编译一个hello world

#include

int main()
{
    printf("hello world!\n");
    return 0;
}
使用static参数编译失败

a.out main.c
$ gcc -static main.c
[localhost test]$ gcc -static main.c
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
默认编译可以

$ gcc main.c
$ ls 
test]$ ldd a.out
    linux-vdso.so.1 =>  (0x00007fff0c3fe000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f5cbb43a000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f5cbb802000)

可以看到a.out还是依赖系统库的。

在另一台机器可以成功将hello world成静态链接的了

[root@ctos helloworld]# gcc -static hello.c
[root@ctos helloworld]# ls
a.out  hello  hello.c  hello.go  mv
[root@ctos helloworld]# ldd a.out
    not a dynamic executable

目前问题集中在报错,不知道原因

[localhost test]$ gcc -static main.c
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status

正确答案

看 /usr/lib 目录有没有 libc.a

$ gcc -static main.c

或

$ gcc -static -lc main.c

今天关于《将C代码编译成完全静态链接出错》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

本文内容来源于互联网,如有侵权请联系删除。

相关文章

更多

热门专题

更多
首页