c++ - 将C代码编译成完全静态链接出错
大家讲道理
大家讲道理 2017-04-17 13:47:14
[C++讨论组]

正常编译一个hello world

#include<stdio.h>

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
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(3)
阿神

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

$ gcc -static main.c

$ gcc -static -lc main.c
PHP中文网

c只能 动态 静态 而且 运行 本地必须有标准库

天蓬老师

拷贝libc.a到当前目录

gcc main.c -L. -lc

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号