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

查看:130
本文介绍了c++ - 将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

解决方案

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

$ gcc -static main.c

$ gcc -static -lc main.c

这篇关于c++ - 将C代码编译成完全静态链接出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆